-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
50 lines (42 loc) · 1.44 KB
/
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
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"github.com/felipedias-dev/fullcycle-go-expert-cobra-cli/internal/database"
"github.com/spf13/cobra"
)
// createCmd represents the create command
func newCreateCmd(categoryDb database.Category) *cobra.Command {
return &cobra.Command{
Use: "create",
Short: "Create a new category",
Long: `Create a new category.`,
RunE: runCreate(categoryDb),
}
}
func runCreate(categoryDb database.Category) RunEFunc {
return func(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
description, _ := cmd.Flags().GetString("description")
_, err := categoryDb.Create(name, description)
if err != nil {
return err
}
return nil
}
}
func init() {
createCmd := newCreateCmd(GetCategoryDb(GetDb()))
categoryCmd.AddCommand(createCmd)
createCmd.Flags().StringP("name", "n", "", "Name of the category")
createCmd.Flags().StringP("description", "d", "", "Description of the category")
createCmd.MarkFlagsRequiredTogether("name", "description")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}