Skip to content

Commit

Permalink
Migrate CLI codebase into package 'cmd'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jun 30, 2018
1 parent a7d1c1f commit 5f5eaff
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ bumper
# Inertia web
node_modules/
public/

# Exceptions
!inertia.go
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The Inertia codebase is split up into several components - this section gives a

### CLI

Inertia's command line application is in the root directory. It is built on top of [cobra](https://github.com/spf13/cobra), a library for building command line applications.
Inertia's command line application is initiated in the root directory, but the majority of the code is in the `cmd` package. It is built on top of [cobra](https://github.com/spf13/cobra), a library for building command line applications.

This code should only include the CLI user interface and code used to manage local assets, such as configuration files - core client logic, functionality, and daemon API interactions should go into the `client` package.

Expand Down
5 changes: 5 additions & 0 deletions cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cmd

[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/ubclaunchpad/inertia/cmd)

This package contains Inertia's CLI codebase.
17 changes: 9 additions & 8 deletions config.go → cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

// initCmd represents the init command
import (
Expand All @@ -13,11 +13,12 @@ import (

// Initialize "inertia" commands regarding basic configuration
func init() {
cmdRoot.AddCommand(cmdInit)
cmdRoot.AddCommand(cmdReset)
cmdRoot.AddCommand(cmdSetConfigProperty)
Root.PersistentFlags().StringVar(&configFilePath, "config", "inertia.toml", "Specify relative path to Inertia configuration")
Root.AddCommand(cmdInit)
Root.AddCommand(cmdReset)
Root.AddCommand(cmdSetConfigProperty)

cmdInit.Flags().String("version", Version, "Specify Inertia daemon version to use")
cmdInit.Flags().String("version", Root.Version, "Specify Inertia daemon version to use")
}

var cmdInit = &cobra.Command{
Expand Down Expand Up @@ -65,7 +66,7 @@ to succeed.`,
}

// Hello world config file!
err = local.InitializeInertiaProject(ConfigFilePath, version, buildType, buildFilePath)
err = local.InitializeInertiaProject(configFilePath, version, buildType, buildFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -94,7 +95,7 @@ var cmdReset = &cobra.Command{
if response != "y" {
log.Fatal("aborting")
}
path, err := common.GetFullPath(ConfigFilePath)
path, err := common.GetFullPath(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -110,7 +111,7 @@ var cmdSetConfigProperty = &cobra.Command{
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down
32 changes: 17 additions & 15 deletions deploy.go → cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bufio"
Expand All @@ -25,7 +25,7 @@ import (
func parseConfigArg() {
for i, arg := range os.Args {
if arg == "--config" {
ConfigFilePath = os.Args[i+1]
configFilePath = os.Args[i+1]
break
}
}
Expand All @@ -36,18 +36,20 @@ func init() {
// This is the only place configuration is read every time an `inertia`
// command is run - check version here.
parseConfigArg() // see parseArgs documentation
config, _, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, _, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
println("[WARNING] Inertia configuration not found in " + ConfigFilePath)
println("[WARNING] Inertia configuration not found in " + configFilePath)
return
}
if config.Version != Version {
if config.Version != Root.Version {
fmt.Printf(
"[WARNING] Configuration version '%s' does not match your Inertia CLI version '%s'\n",
config.Version, Version,
config.Version, Root.Version,
)
}

fmt.Print(config)

// Make a new command for each remote with all associated
// deployment commands.
for _, remote := range config.Remotes {
Expand Down Expand Up @@ -126,7 +128,7 @@ Run 'inertia [REMOTE] init' to gather this information.`,
"verify-ssl", false,
"Verify SSL communications - requires a signed SSL certificate.",
)
cmdRoot.AddCommand(cmd)
Root.AddCommand(cmd)
}
}

Expand All @@ -138,7 +140,7 @@ var cmdDeploymentUp = &cobra.Command{
to be active on your remote - do this by running 'inertia [REMOTE] init'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -202,7 +204,7 @@ var cmdDeploymentDown = &cobra.Command{
Requires project to be online - do this by running 'inertia [REMOTE] up`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -239,7 +241,7 @@ var cmdDeploymentStatus = &cobra.Command{
running 'inertia [REMOTE] up'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -286,7 +288,7 @@ var cmdDeploymentLogs = &cobra.Command{
status' to see what containers are accessible.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -346,7 +348,7 @@ var cmdDeploymentSSH = &cobra.Command{
Long: `Starts up an interact SSH session with your remote.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath)
deployment, err := local.GetClient(remoteName, configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -366,7 +368,7 @@ deployment. Provide a relative path to your file.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -424,7 +426,7 @@ for updates to this repository's remote master branch.`,
remoteName := strings.Split(cmd.Parent().Use, " ")[0]

// Bootstrap needs to write to configuration.
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -456,7 +458,7 @@ remote. Requires Inertia daemon to be active on your remote - do this by
running 'inertia [REMOTE] init'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cmd contains Inertia's CLI logic.
package cmd
8 changes: 4 additions & 4 deletions env.go → cmd/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand All @@ -23,7 +23,7 @@ variables are applied to all deployed containers.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -55,7 +55,7 @@ and persistent environment storage.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand All @@ -79,7 +79,7 @@ var cmdDeploymentEnvList = &cobra.Command{
Short: "List currently set and saved environment variables",
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, ConfigFilePath, cmd)
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion format.go → cmd/format.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion format_test.go → cmd/format_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion input.go → cmd/input.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion input_test.go → cmd/input_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions provision.go → cmd/provision.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down Expand Up @@ -26,7 +26,7 @@ func init() {
cmdProvision.AddCommand(cmdProvisionECS)
cmdProvision.PersistentFlags().StringP("daemon-port", "d", "4303", "Daemon port")
cmdProvision.PersistentFlags().StringArrayP("ports", "p", []string{}, "Ports your project uses")
cmdRoot.AddCommand(cmdProvision)
Root.AddCommand(cmdProvision)
}

var cmdProvision = &cobra.Command{
Expand All @@ -44,7 +44,7 @@ var cmdProvisionECS = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions remote.go → cmd/remote.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"errors"
Expand All @@ -12,7 +12,7 @@ import (

// Initialize 'inertia remote [COMMAND]' commands
func init() {
cmdRoot.AddCommand(cmdRemote)
Root.AddCommand(cmdRemote)
cmdRemote.AddCommand(cmdAddRemote)
cmdRemote.AddCommand(cmdListRemotes)
cmdRemote.AddCommand(cmdRemoveRemote)
Expand Down Expand Up @@ -49,7 +49,7 @@ file. Specify a VPS name.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ var cmdListRemotes = &cobra.Command{
Long: `Lists all currently configured remotes.`,
Run: func(cmd *cobra.Command, args []string) {
verbose, _ := cmd.Flags().GetBool("verbose")
config, _, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, _, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -109,7 +109,7 @@ var cmdRemoveRemote = &cobra.Command{
Long: `Remove a remote from Inertia's configuration file.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -135,7 +135,7 @@ var cmdShowRemote = &cobra.Command{
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, _, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, _, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -156,7 +156,7 @@ var cmdSetRemoteProperty = &cobra.Command{
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, path, err := local.GetProjectConfigFromDisk(ConfigFilePath)
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
if err != nil {
log.Fatal(err)
}
Expand Down
26 changes: 26 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import "github.com/spf13/cobra"

var (
// configFilePath is the relative path to Inertia's configuration file
configFilePath = "inertia.toml"
)

// Root is the base inertia command
var Root = &cobra.Command{
Use: "inertia",
Short: "Inertia is a continuous-deployment scaffold",
Long: `Inertia provides a continuous deployment scaffold for applications.
Initialization involves preparing a server to run an application, then
activating a daemon which will continuously update the production server
with new releases as they become available in the project's repository.
One you have set up a remote with 'inertia remote add [REMOTE]',
use 'inertia [REMOTE] --help' to see what you can do with your remote.
Repository: https://github.com/ubclaunchpad/inertia/
Issue tracker: https://github.com/ubclaunchpad/inertia/issues`,
Version: "latest",
}
Loading

0 comments on commit 5f5eaff

Please sign in to comment.