2024-09-11 11:41:04 +02:00
|
|
|
package hoster_cli
|
2024-08-28 12:31:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-09-13 17:09:21 +02:00
|
|
|
"os/user"
|
2024-09-11 11:18:01 +02:00
|
|
|
"path/filepath"
|
2024-08-28 12:31:17 +02:00
|
|
|
|
2024-09-11 11:30:48 +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,
|
|
|
|
},
|
|
|
|
},
|
2024-09-25 09:17:45 +02:00
|
|
|
Before: func(c *cli.Context) error {
|
|
|
|
conf.LoadConfig(filepath.Join(findHomeDir(), ".hoster.yml"))
|
|
|
|
return nil
|
|
|
|
},
|
2024-09-11 11:14:27 +02:00
|
|
|
Action: commands.CmdNew,
|
|
|
|
},
|
2024-09-25 09:17:45 +02:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-09-11 11:14:27 +02:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 17:09:21 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|