-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
74 lines (63 loc) · 1.92 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"path/filepath"
"strings"
"github.com/XotoX1337/dogo/log"
"github.com/XotoX1337/dogo/terminal"
"github.com/spf13/cobra"
)
var createCmdFileFlag string
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create all or a specific service from a docker-compose.yml file",
Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs),
DisableFlagsInUseLine: true,
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.MarkFlagRequired("file")
}
},
Run: func(cmd *cobra.Command, args []string) {
var service string
if len(args) == 1 {
service = args[0]
}
file, _ := cmd.Flags().GetString("file")
create(service, file)
},
}
func create(service string, file string) {
cmdSlice := []string{
"docker compose",
}
if file != "" {
path := filepath.Join(file)
if !strings.HasSuffix(file, "docker-compose.yml") {
path = filepath.Join(file, "docker-compose.yml")
}
cmdSlice = append(cmdSlice, "-f", path)
}
cmdSlice = append(cmdSlice, "create")
if service != "" {
cmdSlice = append(cmdSlice, service)
}
err := terminal.ShellExecute(strings.Join(cmdSlice, " "), terminal.ShellExecuteOpts{})
if err != nil {
log.Warn(err.Error())
}
}
func init() {
rootCmd.AddCommand(createCmd)
createCmd.Flags().StringVarP(&createCmdFileFlag, "file", "f", "", "specify docker-compose.yml file, defaults to current directory")
// 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")
}