hoster/cmd/hoster_cli/hoster.go

109 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-09-11 11:41:04 +02:00
package hoster_cli
2024-08-28 12:31:17 +02:00
import (
"os"
"os/user"
"path/filepath"
2024-08-28 12:31:17 +02:00
"gitea.codeblob.work/pk/hoster/cmd/hoster_cli/commands"
"gitea.codeblob.work/pk/hoster/internals/conf"
2024-08-28 12:31:17 +02:00
"github.com/kataras/golog"
"github.com/urfave/cli/v2"
)
2024-09-11 11:41:04 +02:00
func Execute() {
2024-08-28 12:31:17 +02:00
app := &cli.App{
2024-08-28 12:42:07 +02:00
Name: "hoster",
Usage: "Hoster",
2024-10-28 09:55:32 +01:00
Version: "1.2.0",
2024-08-28 12:31:17 +02:00
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,
},
2024-09-12 00:18:40 +02:00
&cli.StringFlag{
Name: "custom-path",
Usage: "The path of the project. Defaults to ${projects.root}/${name}.",
Required: false,
},
2024-08-28 12:31:17 +02:00
&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,
},
},
},
2024-10-28 09:55:32 +01:00
{
Name: "port",
Subcommands: []*cli.Command{
{
Name: "free",
Usage: "Get the next free port in the configured range",
Before: func(c *cli.Context) error {
conf.LoadConfig(filepath.Join(findHomeDir(), ".hoster.yml"))
return nil
},
Action: commands.CmdCheckPort,
},
},
},
{
Name: "check",
Usage: "Check if newer versions of docker images are available",
Action: commands.CmdCheckUpdated,
2024-08-28 12:31:17 +02:00
},
},
}
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
}