Use cobra to turn linkctl into a proper cli
Most commands are currently placeholders but version and db init work
This commit is contained in:
		
							
								
								
									
										27
									
								
								cmd/linkctl/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								cmd/linkctl/config.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func configPreRun(cmd *cobra.Command, args []string) error {
 | 
			
		||||
	return setupDb()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func configPostRun(cmd *cobra.Command, args []string) error {
 | 
			
		||||
	return cleanupDb()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func configSetHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	fmt.Println("Not implemented")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func configGetHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	fmt.Println("Not implemented")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func configListHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	fmt.Println("Not implemented")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								cmd/linkctl/db.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								cmd/linkctl/db.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/database"
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/util"
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/version"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func openDB() (*database.DB, error) {
 | 
			
		||||
	paths, err := util.FindDirectories(dbPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return database.Open(paths.DatabaseFile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbPreRun(cmd *cobra.Command, args []string) error {
 | 
			
		||||
	return setupDb()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbPostRun(cmd *cobra.Command, args []string) error {
 | 
			
		||||
	return cleanupDb()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbInitHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	err := db.Initialize(paths.SchemaDir)
 | 
			
		||||
	if err == database.ErrAlreadyInitialized {
 | 
			
		||||
		fmt.Printf("Database %q is already initialized\n", dbPath)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		fmt.Printf("Initialized database %q with schema version %d\n", dbPath, version.SchemaVersion)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("Failed to initialize database %q: %v\n", dbPath, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbBackupHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	fmt.Println("Not implemented")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbUpdateHandler(cmd *cobra.Command, args []string) {
 | 
			
		||||
	fmt.Println("Not implemented")
 | 
			
		||||
}
 | 
			
		||||
@@ -2,17 +2,156 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/database"
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/util"
 | 
			
		||||
	"git.omicron.one/omicron/linkshare/internal/version"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	paths, err := util.FindDirectories("")
 | 
			
		||||
var (
 | 
			
		||||
	dbPath    string
 | 
			
		||||
	verbosity int
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	paths *util.AppPaths
 | 
			
		||||
	db    *database.DB
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func exitIfError(err error) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setupPaths() error {
 | 
			
		||||
	if paths != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	paths_, err := util.FindDirectories(dbPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	paths = paths_
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setupDb() error {
 | 
			
		||||
	if db != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	err := setupPaths()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Paths:")
 | 
			
		||||
	fmt.Println("  Schema:", paths.SchemaDir)
 | 
			
		||||
	fmt.Println("  Database:", paths.DatabaseFile)
 | 
			
		||||
	db_, err := database.Open(dbPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	db = db_
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cleanupDb() error {
 | 
			
		||||
	if db != nil {
 | 
			
		||||
		err := db.Close()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	rootCmd := &cobra.Command{
 | 
			
		||||
		Use:   "linkctl",
 | 
			
		||||
		Short: "LinkShare CLI tool",
 | 
			
		||||
		Long:  `Command line tool to manage your self-hosted LinkShare service.`,
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			cmd.Help()
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	rootCmd.CompletionOptions.DisableDefaultCmd = true
 | 
			
		||||
 | 
			
		||||
	rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Database file path")
 | 
			
		||||
	rootCmd.PersistentFlags().CountVarP(&verbosity, "verbose", "v", "Increase verbosity level")
 | 
			
		||||
 | 
			
		||||
	configCmd := &cobra.Command{
 | 
			
		||||
		Use:   "config",
 | 
			
		||||
		Short: "Configuration commands",
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			cmd.Help()
 | 
			
		||||
		},
 | 
			
		||||
		PersistentPreRunE:  configPreRun,
 | 
			
		||||
		PersistentPostRunE: configPostRun,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	configSetCmd := &cobra.Command{
 | 
			
		||||
		Use:   "set",
 | 
			
		||||
		Short: "Set a configuration value",
 | 
			
		||||
		Run:   configSetHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	configGetCmd := &cobra.Command{
 | 
			
		||||
		Use:   "get",
 | 
			
		||||
		Short: "Get a configuration value",
 | 
			
		||||
		Run:   configGetHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	configListCmd := &cobra.Command{
 | 
			
		||||
		Use:   "list",
 | 
			
		||||
		Short: "List all configuration values",
 | 
			
		||||
		Run:   configListHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	configCmd.AddCommand(configSetCmd, configGetCmd, configListCmd)
 | 
			
		||||
 | 
			
		||||
	dbCmd := &cobra.Command{
 | 
			
		||||
		Use:   "db",
 | 
			
		||||
		Short: "Database commands",
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			cmd.Help()
 | 
			
		||||
		},
 | 
			
		||||
		PersistentPreRunE:  dbPreRun,
 | 
			
		||||
		PersistentPostRunE: dbPostRun,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dbInitCmd := &cobra.Command{
 | 
			
		||||
		Use:   "init",
 | 
			
		||||
		Short: "Initialize the database",
 | 
			
		||||
		Run:   dbInitHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dbBackupCmd := &cobra.Command{
 | 
			
		||||
		Use:   "backup",
 | 
			
		||||
		Short: "Backup the database",
 | 
			
		||||
		Run:   dbBackupHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dbUpdateCmd := &cobra.Command{
 | 
			
		||||
		Use:   "update",
 | 
			
		||||
		Short: "Update the database schema",
 | 
			
		||||
		Run:   dbUpdateHandler,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dbCmd.AddCommand(dbInitCmd, dbBackupCmd, dbUpdateCmd)
 | 
			
		||||
 | 
			
		||||
	versionCmd := &cobra.Command{
 | 
			
		||||
		Use:   "version",
 | 
			
		||||
		Short: "Display version information",
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			version.Print()
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rootCmd.AddCommand(configCmd, dbCmd, versionCmd)
 | 
			
		||||
 | 
			
		||||
	if err := rootCmd.Execute(); err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user