2024-12-21 00:08:16 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2024 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2024-12-16 19:11:36 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/tlinden/anydb/app"
|
|
|
|
|
"github.com/tlinden/anydb/cfg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func completion(cmd *cobra.Command, mode string) error {
|
|
|
|
|
switch mode {
|
|
|
|
|
case "bash":
|
|
|
|
|
return cmd.Root().GenBashCompletion(os.Stdout)
|
|
|
|
|
case "zsh":
|
|
|
|
|
return cmd.Root().GenZshCompletion(os.Stdout)
|
|
|
|
|
case "fish":
|
|
|
|
|
return cmd.Root().GenFishCompletion(os.Stdout, true)
|
|
|
|
|
case "powershell":
|
|
|
|
|
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
|
|
|
|
|
default:
|
2024-12-18 14:06:21 +01:00
|
|
|
return errors.New("invalid shell parameter! Valid ones: bash|zsh|fish|powershell")
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
|
var (
|
|
|
|
|
conf cfg.Config
|
|
|
|
|
ShowVersion bool
|
|
|
|
|
ShowCompletion string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
|
Use: "anydb <command> [options]",
|
|
|
|
|
Short: "anydb",
|
|
|
|
|
Long: `A personal key value store`,
|
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
db, err := app.New(conf.Dbfile, conf.Debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf.DB = db
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
if ShowVersion {
|
2024-12-18 18:44:23 +01:00
|
|
|
fmt.Printf("This is anydb version %s\n", cfg.Version)
|
2024-12-16 19:11:36 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(ShowCompletion) > 0 {
|
|
|
|
|
return completion(cmd, ShowCompletion)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
2024-12-18 14:06:21 +01:00
|
|
|
return errors.New("no command specified")
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 14:23:56 +01:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 19:11:36 +01:00
|
|
|
// options
|
|
|
|
|
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "v", false, "Print program version")
|
|
|
|
|
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
|
2024-12-17 14:23:56 +01:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f",
|
|
|
|
|
filepath.Join(home, ".config", "anydb", "default.db"), "DB file to use")
|
2024-12-16 19:11:36 +01:00
|
|
|
|
|
|
|
|
rootCmd.AddCommand(Set(&conf))
|
2024-12-17 14:23:56 +01:00
|
|
|
rootCmd.AddCommand(List(&conf))
|
2024-12-18 14:06:21 +01:00
|
|
|
rootCmd.AddCommand(Get(&conf))
|
|
|
|
|
rootCmd.AddCommand(Del(&conf))
|
|
|
|
|
rootCmd.AddCommand(Export(&conf))
|
|
|
|
|
rootCmd.AddCommand(Import(&conf))
|
2024-12-20 18:59:59 +01:00
|
|
|
rootCmd.AddCommand(Serve(&conf))
|
2024-12-18 18:44:23 +01:00
|
|
|
rootCmd.AddCommand(Man(&conf))
|
2024-12-16 19:11:36 +01:00
|
|
|
|
2024-12-17 14:23:56 +01:00
|
|
|
err = rootCmd.Execute()
|
2024-12-16 19:11:36 +01:00
|
|
|
if err != nil {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|