hoster/cmd/hoster_cli/hoster.go
2024-09-25 09:18:05 +02:00

95 lines
1.9 KiB
Go

package hoster_cli
import (
"os"
"os/user"
"path/filepath"
"gitea.codeblob.work/pk/hoster/cmd/hoster_cli/commands"
"gitea.codeblob.work/pk/hoster/internals/conf"
"github.com/kataras/golog"
"github.com/urfave/cli/v2"
)
func Execute() {
app := &cli.App{
Name: "hoster",
Usage: "Hoster",
Version: "1.1.0",
Commands: []*cli.Command{
{
Name: "new",
Usage: "Setup a new hosting project.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "The name of the project",
Required: true,
},
&cli.StringFlag{
Name: "custom-path",
Usage: "The path of the project. Defaults to ${projects.root}/${name}.",
Required: false,
},
&cli.StringFlag{
Name: "domain",
Aliases: []string{"d"},
Usage: "The description of the project",
Required: true,
},
},
Before: func(c *cli.Context) error {
conf.LoadConfig(filepath.Join(findHomeDir(), ".hoster.yml"))
return nil
},
Action: commands.CmdNew,
},
{
Name: "docker",
Subcommands: []*cli.Command{
{
Name: "rmi",
Usage: "Select images to remove",
Action: commands.CmdDockerRemoveImages,
},
},
},
{
Name: "check",
Usage: "Check if newer versions of docker images are available",
Action: commands.CmdCheckUpdated,
},
},
}
if err := app.Run(os.Args); err != nil {
golog.Fatal(err)
}
}
func findHomeDir() string {
if os.Getuid() == 0 {
username := os.Getenv("SUDO_USER")
if username == "" {
golog.Fatalf("SUDO_USER environment variable is not set.")
}
u, err := user.Lookup(username)
if err != nil {
golog.Fatal(err)
}
return u.HomeDir
}
dir, err := os.UserHomeDir()
if err != nil {
golog.Fatalf("Error getting home directory: %v", err)
}
return dir
}