/
dial_create.go
65 lines (51 loc) · 1.53 KB
/
dial_create.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
package main
import (
"context"
"flag"
"fmt"
"github.com/benbjohnson/wtf"
"github.com/benbjohnson/wtf/http"
)
// DialCreateCommand is a command for creating dials.
type DialCreateCommand struct {
ConfigPath string
}
// Run executes the command.
func (c *DialCreateCommand) Run(ctx context.Context, args []string) error {
// Create a flag set with parameters for the dial fields.
fs := flag.NewFlagSet("wtf-dial-create", flag.ContinueOnError)
name := fs.String("name", "", "dial name")
attachConfigFlags(fs, &c.ConfigPath)
if err := fs.Parse(args); err != nil {
return err
}
// Load the configuration.
config, err := ReadConfigFile(c.ConfigPath)
if err != nil {
return err
}
// Authenticate the user with the API key from the config.
ctx = wtf.NewContextWithUser(ctx, &wtf.User{APIKey: config.APIKey})
// Build dial from arguments and issue creation request over HTTP.
dial := &wtf.Dial{Name: *name}
svc := http.NewDialService(http.NewClient(config.URL))
if err := svc.CreateDial(ctx, dial); err != nil {
return err
}
// Notify user of their new dial.
fmt.Printf("Your %q dial has been created!\n\n", dial.Name)
fmt.Printf("Please share this URL to invite others to contribute:\n\n")
fmt.Printf("%s\n\n", config.URL+"/invite/"+dial.InviteCode)
return nil
}
// usage print usage information for the command to STDOUT.
func (c *DialCreateCommand) usage() {
fmt.Println(`
Create a new dial.
Usage:
wtf dial create -name "My Dial"
Arguments:
-name NAME
The name of the dial you are creating. Required.
`[1:])
}