diff options
Diffstat (limited to 'conf/create.go')
-rw-r--r-- | conf/create.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/conf/create.go b/conf/create.go new file mode 100644 index 0000000..ec732d2 --- /dev/null +++ b/conf/create.go @@ -0,0 +1,81 @@ +package main + + +import ( + "fmt" + "log" + "strings" + "syscall" + "golang.org/x/term" +) + + +func hiddenPrompt(prompt string) (string, error) { + fmt.Print(prompt) + bytes, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + return "", err + } + + return strings.TrimSpace(string(bytes)), nil +} + + +var createUser = &Command{ + Name: "user", + Execute: func(args []string) { + if len(args) < 1 { + fmt.Println("error: no username provided") + return; + } + username := args[0] + var password string + if len(args) == 1 { + password, err := hiddenPrompt("Enter password: ") + if err != nil { log.Fatal(err) } + confirm, err := hiddenPrompt("Confirm password: ") + if err != nil { log.Fatal(err) } + if password != confirm { + fmt.Println("passwords do not match!") + return + } + } else { + password = args[1] + } + + _, err := p.CreateUser(username, password) + if err != nil { log.Fatal(err) } + fmt.Printf("created user %v\n", username) + }, +} + + +var createEndpoint = &Command{ + Name: "endpoint", + Execute: func(args []string) { + if len(args) < 3 { + fmt.Println("error: name, URI, and address must be specified!") + return + } + + name := args[0] + path := args[1] + addr := args[2] + + _, err := p.CreateEndpoint(name, path, addr) + if err != nil { log.Fatal(err) } + fmt.Printf("created endpoint %v\n", name) + }, +} + + +func CreateInit(parser *Command) { + create := &Command{ + Name: "create", + Branch: true, + } + + create.AddCommand(createUser) + create.AddCommand(createEndpoint) + parser.AddCommand(create) +} |