-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (131 loc) · 4.27 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"log"
"github.com/r4ulcl/nTask/manager"
"github.com/r4ulcl/nTask/worker"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// @title nTask API
// @version v0.1
// @description nTask API documentation
// @contact.name r4ulcl
// @contact.url https://r4ulcl.com
// @contact.email [email protected]
// @license.name GPL-3.0
// @license.url https://github.com/r4ulcl/nTask/blob/main/LICENSE
// @basePath /
// @schemes https http
// @BasePath /
// @Security ApiKeyAuth
// @securityDefinitions.apikey ApiKeyAuth
// @SecurityScheme ApiKeyAuth
// @in header
// @name Authorization
// @description ApiKeyAuth to login
// Config holds configuration parameters.
type Arguments struct {
ConfigFile string
ConfigSSHFile string
Swagger bool
Verbose bool
Debug bool
VerifyAltName bool
}
func main() {
var arguments Arguments
version := "v0.1"
var rootCmd = &cobra.Command{
Use: "nTask",
Short: "Your program description",
Version: version, // Set the version here
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return validateGlobalFlags(cmd.Flags(), &arguments)
},
}
// Add global flags to the root command
rootCmd.PersistentFlags().BoolP("swagger", "s", false, "Start the swagger endpoint (/swagger)")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Set verbose mode")
rootCmd.Flags().BoolP("version", "V", false, "Version for nTask")
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Set debug mode")
rootCmd.PersistentFlags().BoolP("verifyAltName", "a", false, "Set verifyAltName to true")
// Add manager subcommand
var managerCmd = &cobra.Command{
Use: "manager",
Short: "Run the manager module",
Run: func(cmd *cobra.Command, args []string) {
log.Println("Manager:")
managerStart(&arguments)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return validateSubcommandFlags(cmd.Flags(), &arguments)
},
}
// Add flags specific to the manager subcommand
managerCmd.Flags().StringVarP(&arguments.ConfigFile,
"configFile", "c", "", "Path to the config file (default: manager.conf)")
managerCmd.Flags().StringVarP(&arguments.ConfigSSHFile,
"configSSHFile", "f", "", "Path to the config SSH file (default: empty)")
// Add worker subcommand
var workerCmd = &cobra.Command{
Use: "worker",
Short: "Run the worker module",
Run: func(cmd *cobra.Command, args []string) {
log.Println("Worker:")
workerStart(&arguments)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return validateSubcommandFlags(cmd.Flags(), &arguments)
},
}
// Add flags specific to the worker subcommand
workerCmd.Flags().StringVarP(&arguments.ConfigFile,
"configFile", "c", "", "Path to the config file (default: worker.conf)")
// Add subcommands to the root command
rootCmd.AddCommand(managerCmd, workerCmd)
// Execute the commands
if err := rootCmd.Execute(); err != nil {
log.Println(err)
}
}
func managerStart(arguments *Arguments) {
// Use config parameters to start the manager
if arguments.ConfigFile == "" {
arguments.ConfigFile = "manager.conf"
}
manager.StartManager(arguments.Swagger, arguments.ConfigFile,
arguments.ConfigSSHFile, arguments.VerifyAltName, arguments.Verbose, arguments.Debug)
}
func workerStart(arguments *Arguments) {
// Use config parameters to start the worker
if arguments.ConfigFile == "" {
arguments.ConfigFile = "worker.conf"
}
worker.StartWorker(arguments.Swagger, arguments.ConfigFile,
arguments.VerifyAltName, arguments.Verbose, arguments.Debug)
}
func validateGlobalFlags(flags *pflag.FlagSet, arguments *Arguments) error {
var err error
arguments.Swagger, err = flags.GetBool("swagger")
if err != nil {
return fmt.Errorf("error getting 'swagger' flag: %w", err)
}
arguments.Verbose, err = flags.GetBool("verbose")
if err != nil {
return fmt.Errorf("error getting 'verbose' flag: %w", err)
}
arguments.Debug, err = flags.GetBool("debug")
if err != nil {
return fmt.Errorf("error getting 'debug' flag: %w", err)
}
// If its debug mode its also verbose mode
if arguments.Debug {
arguments.Verbose = true
}
arguments.VerifyAltName, err = flags.GetBool("verifyAltName")
return err
}
func validateSubcommandFlags(flags *pflag.FlagSet, arguments *Arguments) error {
return validateGlobalFlags(flags, arguments)
}