/
create_client.go
62 lines (54 loc) · 1.68 KB
/
create_client.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
package cmd
import (
"github.com/bwmarrin/snowflake"
"github.com/jinzhu/gorm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/meowpub/meow/config"
"github.com/meowpub/meow/models"
)
// createClientCmd represents the createClient command
var createClientCmd = &cobra.Command{
Use: "client",
Short: "Creates an OAuth client application",
Long: `Creates an OAuth client application.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Collect flags upfront.
name := viper.GetString("create.client.name")
desc := viper.GetString("create.client.description")
ownerID := snowflake.ID(viper.GetInt64("create.client.owner-id"))
redirect := viper.GetString("create.client.redirect-uri")
// Create a client object.
clData := models.ClientUserData{
Name: name,
Description: desc,
}
if ownerID != 0 {
clData.OwnerID = &ownerID
}
cl, err := models.NewClient(clData, redirect)
if err != nil {
return err
}
dump(cl)
// Connect to the database.
db, err := gorm.Open("postgres", config.DB())
if err != nil {
return err
}
// Insert the objects inside a transaction.
tx := db.Begin()
if err := models.NewClientStore(db).Create(cl); err != nil {
return err
}
return tx.Commit().Error
},
}
func init() {
createCmd.AddCommand(createClientCmd)
createClientCmd.Flags().StringP("name", "n", "", "human-readable name (required)")
createClientCmd.Flags().StringP("description", "d", "", "human-readable description")
createClientCmd.Flags().Int64P("owner-id", "O", 0, "ID of the owning user")
createClientCmd.Flags().StringP("redirect-uri", "r", "", "redirect URI (required)")
bindPFlags("create.client", createClientCmd.Flags())
}