gomanager/cmd/setup.go
2023-12-01 23:02:33 +01:00

56 lines
1.4 KiB
Go

package cmd
import (
"context"
"errors"
"fmt"
"os"
"code.icod.de/dalu/gomanager/ent"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "sets up the program",
RunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return errors.New("this program must be ran as root")
}
_, e := os.Stat("/var/lib/gomanager")
if errors.Is(e, os.ErrNotExist) {
err := os.MkdirAll("/var/lib/gomanager", 0777)
if err != nil {
return err
}
}
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
return fmt.Errorf("failed creating schema resources: %v", err)
}
return nil
},
}
func init() {
rootCmd.AddCommand(setupCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setupCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// setupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}