refactor(cli): moved commands and cli out of main

This commit is contained in:
Roxie Gibson 2021-07-15 03:44:07 +01:00
parent 2aa9029893
commit 9a0bd6dc11
Signed by untrusted user: roxxers
GPG Key ID: 5D0140EDEE123F4D
5 changed files with 206 additions and 77 deletions

74
cli/app.go Normal file
View File

@ -0,0 +1,74 @@
package cli
import "github.com/urfave/cli/v2"
var appNewCommand = &cli.Command{
Name: "new",
}
var appDeployCommand = &cli.Command{
Name: "deploy",
}
var appUndeployCommand = &cli.Command{
Name: "undeploy",
}
var appBackupCommand = &cli.Command{
Name: "backup",
}
var appRestoreCommand = &cli.Command{
Name: "restore",
}
var appListCommand = &cli.Command{
Name: "list",
}
var appCheckCommand = &cli.Command{
Name: "check",
}
var appCpCommand = &cli.Command{
Name: "cp",
}
var appConfigCommand = &cli.Command{
Name: "config",
}
var appLogsCommand = &cli.Command{
Name: "logs",
}
var appPsCommand = &cli.Command{
Name: "ps",
}
var appRemoveCommand = &cli.Command{
Name: "remove",
}
var appRunCommand = &cli.Command{
Name: "run",
}
var appRollbackCommand = &cli.Command{
Name: "rollback",
}
var appSecretCommand = &cli.Command{
Name: "secret",
}
var AppCommand = &cli.Command{
Name: "app",
HideHelp: true,
Subcommands: []*cli.Command{
appNewCommand,
appConfigCommand,
appDeployCommand,
appUndeployCommand,
appBackupCommand,
appRestoreCommand,
appRemoveCommand,
appCheckCommand,
appListCommand,
appPsCommand,
appLogsCommand,
appCpCommand,
appRunCommand,
appRollbackCommand,
appSecretCommand,
},
}

39
cli/cli.go Normal file
View File

@ -0,0 +1,39 @@
package cli
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func RunApp(version, commit string) {
app := &cli.App{
Name: "abra",
Usage: "The cooperative cloud utility belt 🎩🐇",
Version: fmt.Sprintf("%s-%s", version, commit[:7]),
Commands: []*cli.Command{
AppCommand,
ServerCommand,
{
Name: "recipe",
HideHelp: true,
Subcommands: []*cli.Command{
{
Name: "list",
},
{
Name: "create",
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

3
cli/common.go Normal file
View File

@ -0,0 +1,3 @@
package cli
const emptyArgsUsage = " " // Removes "[arguments]" from help. Empty str's are ignored

81
cli/server.go Normal file
View File

@ -0,0 +1,81 @@
package cli
import (
"fmt"
"github.com/urfave/cli/v2"
)
var serverListCommand = &cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List locally-defined servers.",
ArgsUsage: emptyArgsUsage,
HideHelp: true,
}
var serverAddCommand = &cli.Command{
Name: "add",
Usage: "Add a server, reachable on <host>.",
ArgsUsage: "<host> [<user>] [<port>]",
Description: "[<user>], [<port>] SSH connection details",
}
var serverNewCommand = &cli.Command{
Name: "new",
Usage: "Creates a VPS from a provider in your terminal!",
Description: "Use a provider plugin to create an actual new server resource (VPS or otherwise) which can then be used to house a new Co-op Cloud installation.",
ArgsUsage: "<provider>",
HideHelp: true,
}
var serverRemoveCommand = &cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove server <host>",
HideHelp: true,
}
var serverInitCommand = &cli.Command{
Name: "init",
Usage: "Set up a server for Docker swarm",
HideHelp: true,
ArgsUsage: "<host>",
Description: `This initialisation explicitly chooses the "single host swarm" mode
which uses the default IPv4 address as the advertising address. This
can be re-configured later for more advanced use cases.
POWERED BY
docker swarm init
docker network create ...`,
}
var serverAppsCommand = &cli.Command{
Name: "apps",
Usage: `Alias for "abra app ls --server <host>"`,
HideHelp: true,
ArgsUsage: " ", // Removes "[arguments]" from help. Empty str's are ignored
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "status",
Value: false,
},
},
}
// Reminder: The list commands are in is the order they appear in the help menu
var ServerCommand = &cli.Command{
Name: "server",
ArgsUsage: "<host>",
Usage: "Interact with the servers hosting your Coop-Cloud apps",
HideHelp: true,
Subcommands: []*cli.Command{
serverNewCommand,
serverInitCommand,
serverAddCommand,
serverListCommand,
serverRemoveCommand,
serverAppsCommand,
},
Action: func(c *cli.Context) error { fmt.Println("server"); return nil },
}

View File

@ -1,87 +1,19 @@
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
import "coopcloud.tech/abra/cli"
var Version string
var Commit string
func main() {
app := &cli.App{
Name: "abra",
Usage: "The cooperative cloud utility belt 🎩🐇",
Version: fmt.Sprintf("%s-%s", Version, Commit[:7]),
Commands: []*cli.Command{
{
Name: "server",
ArgsUsage: "<host>",
Usage: "Interact with the servers hosting your Coop-Cloud apps",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List locally-defined servers.",
ArgsUsage: " ", // Removes "[arguments]" from help. Empty str's are ignored
HideHelp: true,
},
{
Name: "add",
Usage: "Add a server, reachable on <host>.",
ArgsUsage: "<host> [<user>] [<port>]",
Description: "[<user>], [<port>] SSH connection details",
HideHelp: true,
},
{
Name: "new",
Usage: "Creates a VPS from a provider in your terminal!",
Description: "Use a provider plugin to create an actual new server resource (VPS or otherwise) which can then be used to house a new Co-op Cloud installation.",
ArgsUsage: "<provider>",
HideHelp: true,
},
{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove server <host>",
HideHelp: true,
},
{
Name: "init",
Usage: "Set up a server for Docker swarm",
HideHelp: true,
ArgsUsage: "<host>",
Description: `This initialisation explicitly chooses the "single host swarm" mode
which uses the default IPv4 address as the advertising address. This
can be re-configured later for more advanced use cases.
POWERED BY
docker swarm init
docker network create ...`,
},
{
Name: "apps",
Usage: `Alias for "abra app ls --server <host>"`,
HideHelp: true,
ArgsUsage: " ", // Removes "[arguments]" from help. Empty str's are ignored
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "status",
Value: false,
},
},
},
},
Action: func(c *cli.Context) error { fmt.Println("server"); return nil },
},
},
// If not set in the ld-flags
if Version == "" {
Version = "dev"
}
if Commit == "" {
Commit = " "
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
cli.RunApp(Version, Commit)
}