2024-01-26 19:32:21 -06:00
/ *
2026-03-08 22:59:33 -05:00
Copyright © 2026 Dan Jones < danjones @ goodevilgenius . org >
2024-01-26 19:32:21 -06:00
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/>.
* /
2026-02-28 16:45:57 -06:00
package cli
2024-01-26 19:32:21 -06:00
import (
2026-03-08 22:59:33 -05:00
"context"
2024-01-26 19:32:21 -06:00
"os"
2024-02-04 18:19:19 -06:00
"codeberg.org/danjones000/my-log/config"
2024-01-26 19:32:21 -06:00
"github.com/spf13/cobra"
)
2026-02-28 16:45:57 -06:00
var RootCmd = & cobra . Command {
2024-01-26 19:32:21 -06:00
Use : "my-log" ,
Short : "A brief description of your application" ,
2026-03-08 22:59:33 -05:00
PersistentPreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2026-03-09 16:48:58 -05:00
ctx , v , err := config . New ( cmd . Context ( ) , configPath )
2026-03-08 22:59:33 -05:00
if err != nil {
return err
}
for k , val := range configValues {
v . Set ( k , val )
}
cmd . SetContext ( ctx )
return nil
} ,
2024-01-26 19:32:21 -06:00
}
2026-03-08 22:59:33 -05:00
func Execute ( ctx context . Context ) {
err := RootCmd . ExecuteContext ( ctx )
2024-01-26 19:32:21 -06:00
if err != nil {
os . Exit ( 1 )
}
}
2026-03-08 22:59:33 -05:00
var configPath string
var configValues map [ string ] string
2024-01-26 19:32:21 -06:00
func init ( ) {
2026-03-09 16:48:58 -05:00
path := os . Getenv ( "MYLOG_CONFIG_PATH" )
if path == "" {
path = config . DefaultPath ( )
}
RootCmd . PersistentFlags ( ) . StringVarP ( & configPath , "config" , "c" , path , "config file" )
2026-03-09 13:38:13 -05:00
RootCmd . PersistentFlags ( ) . StringToStringVarP ( & configValues , "config-value" , "v" , nil , "Override config values. Use dot syntax to specify key. E.g. -v formatters.preferred=json" )
2024-01-26 19:32:21 -06:00
}