-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathcustom_config_install.go
121 lines (110 loc) · 3.08 KB
/
custom_config_install.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//go:build darwin || linux || windows
package object
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func configInstallCommand() *core.Command {
type installArgs struct {
Region scw.Region
Type s3tool
Name string
}
return &core.Command{
Namespace: "object",
Resource: "config",
Verb: "install",
Short: "Install a S3 tool configuration file to its default location",
Long: "Install a S3 tool configuration file to its default location.",
ArgsType: reflect.TypeOf(installArgs{}),
ArgSpecs: []*core.ArgSpec{
{
Name: "type",
Short: "Type of S3 tool you want to generate a config for",
Required: true,
EnumValues: supportedTools.ToStringArray(),
},
{
Name: "name",
Short: "Name of the s3 remote you want to generate",
Required: false,
Default: core.DefaultValueSetter("scaleway"),
},
{
Name: "project-id",
Short: "Scaleway project ID to use with IAM Access Key syntax",
Required: false,
ValidateFunc: core.ValidateProjectID(),
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms),
},
Examples: []*core.Example{
{
Short: "Install a s3cmd config file for Paris region",
ArgsJSON: `{"type": "s3cmd", "region": "fr-par"}`,
},
{
Short: "Install a rclone config file for default region",
ArgsJSON: `{"type": "rclone"}`,
},
{
Short: "Install a mc (minio) config file for default region",
ArgsJSON: `{"type": "mc"}`,
},
},
SeeAlsos: []*core.SeeAlso{
{
Short: "Generate a S3 tool configuration file",
Command: "scw object config get",
},
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
args := argsI.(*installArgs)
config, err := newS3Config(ctx, args.Region, args.Name)
if err != nil {
return "", err
}
newConfig, err := config.getConfigFile(args.Type)
if err != nil {
return "", err
}
configPath, err := config.getPath(args.Type)
if err != nil {
return "", err
}
// Ask whether to remove previous configuration file if it exists
if _, err := os.Stat(configPath); err == nil {
doIt, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Ctx: ctx,
Prompt: "Do you want to overwrite the existing configuration file (" + configPath + ")?",
DefaultValue: false,
})
if err != nil {
return nil, err
}
if !doIt {
return nil, errors.New("installation aborted by user")
}
}
// Ensure the subfolders for the configuration files are all created
err = os.MkdirAll(filepath.Dir(configPath), 0o755)
if err != nil {
return "", err
}
// Write the configuration file
err = os.WriteFile(configPath, []byte(newConfig), 0o600)
if err != nil {
return "", err
}
return &core.SuccessResult{
Message: "Configuration file successfully installed at " + configPath,
}, nil
},
}
}