67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
/*
|
|
Copyright © 2026 Dan Jones <danjones@goodevilgenius.org>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var RootCmd = &cobra.Command{
|
|
Use: "my-log",
|
|
Short: "A brief description of your application",
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
ctx, v, err := config.New(cmd.Context())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if configPath != "" {
|
|
v.SetConfigFile(configPath)
|
|
err := v.ReadInConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for k, val := range configValues {
|
|
v.Set(k, val)
|
|
}
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func Execute(ctx context.Context) {
|
|
err := RootCmd.ExecuteContext(ctx)
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
var configPath string
|
|
var configValues map[string]string
|
|
|
|
func init() {
|
|
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file")
|
|
RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v formatters.preferred=json")
|
|
}
|