-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.go
154 lines (137 loc) · 5.02 KB
/
init.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package credentials
import (
"errors"
"fmt"
"os"
"strings"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/arduino"
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/go-paths-helper"
"github.com/manifoldco/promptui"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var initFlags struct {
destDir string
overwrite bool
format string
}
func initInitCommand() *cobra.Command {
initCommand := &cobra.Command{
Use: "init",
Short: "Initialize a credentials file",
Long: "Initialize an Arduino IoT Cloud CLI credentials file",
Run: runInitCommand,
}
initCommand.Flags().StringVar(&initFlags.destDir, "dest-dir", "", "Sets where to save the credentials file")
initCommand.Flags().BoolVar(&initFlags.overwrite, "overwrite", false, "Overwrite existing credentials file")
initCommand.Flags().StringVar(&initFlags.format, "file-format", "yaml", "Format of the credentials file, can be {yaml|json}")
return initCommand
}
func runInitCommand(cmd *cobra.Command, args []string) {
logrus.Info("Initializing credentials file")
// Get default destination directory if it's not passed
if initFlags.destDir == "" {
credPath, err := arduino.DataDir()
if err != nil {
feedback.Errorf("Error during credentials init: cannot retrieve arduino default directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
// Create arduino default directory if it does not exist
if credPath.NotExist() {
if err = credPath.MkdirAll(); err != nil {
feedback.Errorf("Error during credentials init: cannot create arduino default directory %s: %v", credPath, err)
os.Exit(errorcodes.ErrGeneric)
}
}
initFlags.destDir = credPath.String()
}
// Validate format flag
initFlags.format = strings.ToLower(initFlags.format)
if initFlags.format != "json" && initFlags.format != "yaml" {
feedback.Error("Error during credentials init: format is not valid, provide 'json' or 'yaml'")
os.Exit(errorcodes.ErrGeneric)
}
// Check that the destination directory is valid and build the credentials file path
credPath, err := paths.New(initFlags.destDir).Abs()
if err != nil {
feedback.Errorf("Error during credentials init: cannot retrieve absolute path of %s: %v", initFlags.destDir, err)
os.Exit(errorcodes.ErrGeneric)
}
if !credPath.IsDir() {
feedback.Errorf("Error during credentials init: %s is not a valid directory", credPath)
os.Exit(errorcodes.ErrGeneric)
}
credFile := credPath.Join(config.CredentialsFilename + "." + initFlags.format)
if !initFlags.overwrite && credFile.Exist() {
feedback.Errorf("Error during credentials init: %s already exists, use '--overwrite' to overwrite it",
credFile)
os.Exit(errorcodes.ErrGeneric)
}
// Take needed credentials starting an interactive mode
feedback.Print("To obtain your API credentials visit https://create.arduino.cc/iot/integrations")
id, key, err := paramsPrompt()
if err != nil {
feedback.Errorf("Error during credentials init: cannot take credentials params: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
// Write the credentials file
newSettings := viper.New()
newSettings.SetConfigPermissions(os.FileMode(0600))
newSettings.Set("client", id)
newSettings.Set("secret", key)
if err := newSettings.WriteConfigAs(credFile.String()); err != nil {
feedback.Errorf("Error during credentials init: cannot write credentials file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
feedback.Printf("Credentials file successfully initialized at: %s", credFile)
}
func paramsPrompt() (id, key string, err error) {
prompt := promptui.Prompt{
Label: "Please enter the Client ID",
Validate: func(s string) error {
if len(s) != config.ClientIDLen {
return errors.New("client-id not valid")
}
return nil
},
}
id, err = prompt.Run()
if err != nil {
return "", "", fmt.Errorf("client prompt fail: %w", err)
}
prompt = promptui.Prompt{
Mask: '*',
Label: "Please enter the Client Secret",
Validate: func(s string) error {
if len(s) != config.ClientSecretLen {
return errors.New("client secret not valid")
}
return nil
},
}
key, err = prompt.Run()
if err != nil {
return "", "", fmt.Errorf("client secret prompt fail: %w", err)
}
return id, key, nil
}