-
Notifications
You must be signed in to change notification settings - Fork 17
/
uploadfile.go
67 lines (59 loc) · 1.59 KB
/
uploadfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package apit
import (
"context"
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
cmds "github.com/saucelabs/saucectl/internal/cmd"
"github.com/saucelabs/saucectl/internal/http"
"github.com/saucelabs/saucectl/internal/segment"
"github.com/saucelabs/saucectl/internal/usage"
)
func UploadFileCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "upload-file FILENAME [--project PROJECT_NAME]",
Short: "Upload a file in vault",
Long: `Upload a file in a project's vault.
Use [--project] to specify the project by its name or run without [--project] to choose from a list of projects.
`,
SilenceUsage: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return errors.New("no file name specified")
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
err := http.CheckProxy()
if err != nil {
return fmt.Errorf("invalid HTTP_PROXY value")
}
tracker := segment.DefaultTracker
go func() {
tracker.Collect(
cases.Title(language.English).String(cmds.FullName(cmd)),
usage.Properties{}.SetFlags(cmd.Flags()),
)
_ = tracker.Close()
}()
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
fd, err := os.Open(name)
if err != nil {
return err
}
_, err = apitesterClient.PutVaultFile(context.Background(), selectedProject.ID, name, fd)
if err != nil {
return err
}
fmt.Printf("File %q has been successfully stored.\n", name)
return nil
},
}
return cmd
}