Skip to content

Commit a59807d

Browse files
committed
remove PreRunE in cobra, make generic functionality for fileshare
functions
1 parent 761c5b7 commit a59807d

File tree

16 files changed

+105
-128
lines changed

16 files changed

+105
-128
lines changed

app.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/chanmaoganda/fileshare/internal/fileshare"
78
"github.com/wailsapp/wails/v2/pkg/runtime"
89
)
910

@@ -49,3 +50,7 @@ func (a *App) OpenFileSelectionDialog() (string, error) {
4950

5051
return selection, nil
5152
}
53+
54+
func (a *App) UploadFile(args []string) {
55+
fileshare.Upload(context.TODO(), args)
56+
}

cmd/cache/clean.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import (
44
"os"
55

66
"github.com/chanmaoganda/fileshare/internal/config"
7-
"github.com/chanmaoganda/fileshare/internal/core/setup"
87
"github.com/sirupsen/logrus"
98
"github.com/spf13/cobra"
109
)
1110

1211
var cleanCmd = &cobra.Command{
13-
Use: "clean",
14-
Short: "Cleans db file and cache folder by config.yml, if not set then clean the default ones(default.db, $HOME/.fileshare)",
15-
PreRunE: setup.Setup,
12+
Use: "clean",
13+
Short: "Cleans db file and cache folder by config.yml, if not set then clean the default ones(default.db, $HOME/.fileshare)",
1614
Run: func(cmd *cobra.Command, args []string) {
17-
1815
cleanCache()
1916
},
2017
}

cmd/client/download.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package client
22

33
import (
4-
"github.com/chanmaoganda/fileshare/internal/core/setup"
54
"github.com/chanmaoganda/fileshare/internal/fileshare"
65
"github.com/spf13/cobra"
76
)
87

98
var DownloadCmd = &cobra.Command{
10-
Use: "download <checksum256 | linkcode>",
11-
Short: "Download file, either with sharelink code or file checksum256 hash",
12-
Args: cobra.MinimumNArgs(1),
13-
PreRunE: setup.Setup,
9+
Use: "download <checksum256 | linkcode>",
10+
Short: "Download file, either with sharelink code or file checksum256 hash",
11+
Args: cobra.MinimumNArgs(1),
1412
Run: func(cmd *cobra.Command, args []string) {
1513
fileshare.Download(cmd.Context(), args)
1614
},

cmd/client/linkgen.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package client
22

33
import (
4-
"github.com/chanmaoganda/fileshare/internal/core/setup"
54
"github.com/chanmaoganda/fileshare/internal/fileshare"
65
"github.com/spf13/cobra"
76
)
87

98
var ShareLinkGenCmd = &cobra.Command{
10-
Use: "linkgen <filename> <checksum256> <expire days(optional)>",
11-
Short: "Generates sharelink code for friends to easily download",
12-
Args: cobra.MinimumNArgs(2),
13-
PreRunE: setup.Setup,
9+
Use: "linkgen <filename> <checksum256> <expire days(optional)>",
10+
Short: "Generates sharelink code for friends to easily download",
11+
Args: cobra.MinimumNArgs(2),
1412
Run: func(cmd *cobra.Command, args []string) {
1513
fileshare.LinkGen(cmd.Context(), args)
1614
},

cmd/client/upload.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package client
22

33
import (
4-
"github.com/chanmaoganda/fileshare/internal/core/setup"
54
"github.com/chanmaoganda/fileshare/internal/fileshare"
65
"github.com/spf13/cobra"
76
)
87

98
var UploadCmd = &cobra.Command{
10-
Use: "upload <filepath>",
11-
Short: "Uploads the file, requires the filepath as argument",
12-
Args: cobra.MinimumNArgs(1),
13-
PreRunE: setup.Setup,
9+
Use: "upload <filepath>",
10+
Short: "Uploads the file, requires the filepath as argument",
11+
Args: cobra.MinimumNArgs(1),
1412
Run: func(cmd *cobra.Command, args []string) {
1513
fileshare.Upload(cmd.Context(), args)
1614
},

cmd/server/server.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package server
22

33
import (
4-
"github.com/chanmaoganda/fileshare/internal/core/setup"
54
"github.com/chanmaoganda/fileshare/internal/fileshare"
65
"github.com/spf13/cobra"
76
)
87

98
var ServerCmd = &cobra.Command{
10-
Use: "server",
11-
Short: "Starts fileshare server",
12-
PreRunE: setup.Setup,
9+
Use: "server",
10+
Short: "Starts fileshare server",
1311
Run: func(cmd *cobra.Command, args []string) {
1412
fileshare.Server()
1513
},

frontend/src/App.tsx

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import React, { useState } from "react";
22
import "./App.css";
33

4-
import {
5-
OpenFileSelectionDialog,
6-
OpenMultipleFilesSelectionDialog,
7-
} from "../wailsjs/go/main/App";
4+
import { OpenFileSelectionDialog, UploadFile } from "../wailsjs/go/main/App";
85

96
function App() {
107
const [selectedFilePath, setSelectedFilePath] = useState<string>("");
@@ -26,27 +23,14 @@ function App() {
2623
}
2724
};
2825

29-
const handleOpenMultipleFiles = async () => {
30-
setError("");
31-
try {
32-
const paths = await OpenMultipleFilesSelectionDialog();
33-
if (paths && paths.length > 0) {
34-
setSelectedFilePaths(paths);
35-
} else {
36-
setSelectedFilePaths(["No files selected."]);
37-
}
38-
} catch (err: any) {
39-
setError(
40-
`Error opening multiple files dialog: ${err.message || String(err)}`,
41-
);
42-
setSelectedFilePaths(["Error!"]);
43-
}
26+
const handleUpload = async () => {
27+
UploadFile([selectedFilePath]);
4428
};
4529

4630
return (
4731
<div className="App">
4832
<header className="App-header">
49-
<h1>Wails File Dialog Demo</h1>
33+
<h1>Wails Fileshare Client</h1>
5034

5135
{error && <p style={{ color: "red" }}>{error}</p>}
5236

@@ -56,10 +40,7 @@ function App() {
5640
</div>
5741

5842
<div className="card">
59-
<button onClick={handleOpenMultipleFiles}>
60-
Open Multiple Files Dialog
61-
</button>
62-
<p>Selected Files: {selectedFilePaths.join(", ")}</p>
43+
<button onClick={handleUpload}>Upload This File</button>
6344
</div>
6445
</header>
6546
</div>

frontend/wailsjs/go/main/App.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
// This file is automatically generated. DO NOT EDIT
33

44
export function OpenFileSelectionDialog():Promise<string>;
5+
6+
export function UploadFile(arg1:Array<string>):Promise<void>;

frontend/wailsjs/go/main/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
export function OpenFileSelectionDialog() {
66
return window['go']['main']['App']['OpenFileSelectionDialog']();
77
}
8+
9+
export function UploadFile(arg1) {
10+
return window['go']['main']['App']['UploadFile'](arg1);
11+
}

frontend/wailsjs/runtime/package.json

100755100644
File mode changed.

0 commit comments

Comments
 (0)