Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated go.mod #296

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cmd/app/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package app

import (
"errors"

"github.com/spf13/cobra"
)

var AppCmd = &cobra.Command{
Use: "app",
Aliases: []string{"apps, application, applications"},
Short: "Manage Applications inside your Civo account",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("a valid subcommand is required")
},
}

// var appDomainCmd = &cobra.Command{
// Use: "domain",
// Aliases: []string{"domains"},
// Short: "Details of your application domains",
// RunE: func(cmd *cobra.Command, args []string) error {
// err := cmd.Help()
// if err != nil {
// return err
// }
// return errors.New("a valid subcommand is required")
// },
// }

var appConfigCmd = &cobra.Command{
Use: "config",
Aliases: []string{"conf"},
Short: "Configure your application",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("a valid subcommand is required")
},
}

func init() {
AppCmd.AddCommand(appListCmd)
AppCmd.AddCommand(appRemoveCmd)
AppCmd.AddCommand(appShowCmd)
AppCmd.AddCommand(appCreateCmd)
AppCmd.AddCommand(appUpdateCmd)
AppCmd.AddCommand(appConfigCmd)
appConfigCmd.AddCommand(appConfigShowCmd)
appConfigCmd.AddCommand(appConfigSetCmd)
appConfigSetCmd.Flags().StringVarP(&configName, "name", "n", "", "The name of the environment variable you want to set.")
appConfigSetCmd.Flags().StringVarP(&configValue, "value", "v", "", "The value of the environment variable you want to set.")

// Create flags
appCreateCmd.Flags().StringVarP(&appSize, "size", "s", "", "Size of the application")
appCreateCmd.Flags().StringVarP(&gitURL, "git-url", "g", "", "URL of the git repo")
appCreateCmd.Flags().StringVarP(&image, "image", "i", "", "Container Image to pull")
appCreateCmd.Flags().StringVarP(&branchName, "branch", "b", "", "Branch for the git repo")
appCreateCmd.Flags().StringVarP(&tagName, "tag", "t", "", "Tag for the git repo")

// Update flags
appUpdateCmd.Flags().StringVarP(&name, "name", "n", "", "Updated Name of the application")
appUpdateCmd.Flags().StringVarP(&appSize, "size", "s", "", "Updated Size of the application")
appUpdateCmd.Flags().StringVarP(&firewallID, "firewall-id", "", "", "Firewall ID of the application")
appUpdateCmd.Flags().StringVarP(&processType, "process-type", "t", "", "The type of process you want to scale. E.g. web, worker, etc.")
appUpdateCmd.Flags().IntVarP(&processCount, "process-count", "c", 0, "The number by which you want to scale the process. E.g. 2, 3, etc.")
}
73 changes: 73 additions & 0 deletions cmd/app/application_config_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package app

import (
"fmt"

"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"

"os"

"github.com/spf13/cobra"
)

var configName, configValue string

var appConfigSetCmd = &cobra.Command{
Use: "set",
Args: cobra.MinimumNArgs(1),
Short: "Set application config",
Example: "civo app config set APP_NAME --name=foo --value=bar",
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

findApp, err := client.FindApplication(args[0])
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

// TODO: If the user has a .env, env vars will be picked up from there.
// Add this in help menu for config.
updatedConfig := make([]civogo.EnvVar, 0)
var varFound bool
for _, envVar := range findApp.Config.Env {
if envVar.Name == configName {
varFound = true
envVar.Value = configValue
}
updatedConfig = append(updatedConfig, envVar)
}
if !varFound {
updatedConfig = append(updatedConfig, civogo.EnvVar{
Name: configName,
Value: configValue,
})
}

app, err := client.UpdateApplication(findApp.ID, &civogo.UpdateApplicationRequest{
EnvVars: updatedConfig,
})
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

ow := utility.NewOutputWriterWithMap(map[string]string{"id": app.ID, "name": app.Name})

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("Application %s's config has been updated.\n", utility.Green(app.Name))
}
},
}
49 changes: 49 additions & 0 deletions cmd/app/application_config_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app

import (
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"

"os"

"github.com/spf13/cobra"
)

var appConfigShowCmd = &cobra.Command{
Use: "show",
Aliases: []string{"get", "inspect"},
Args: cobra.MinimumNArgs(1),
Short: "Show application config",
Example: "civo app config show APP_NAME",
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

app, err := client.FindApplication(args[0])
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

ow := utility.NewOutputWriter()
ow.StartLine()

for _, env := range app.Config.Env {
ow.AppendDataWithLabel("name", env.Name, "Name")
ow.AppendDataWithLabel("value", env.Value, "Value")
}

switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteKeyValues()
}
},
}
150 changes: 150 additions & 0 deletions cmd/app/application_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package app

import (
"fmt"
"os"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
)

var appSize, gitURL, image, tagName, branchName string
var wait bool

var appCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: "civo app create APP_NAME [flags]",
Short: "Create a new application",
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

// TODO: Add network and firewall flags

client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

config, err := client.NewApplicationConfig()
if err != nil {
utility.Error("Unable to create a new config for the app %s", err)
os.Exit(1)
}

config.PublicIPv4Required = true

if image == "" && gitURL == "" {
utility.Error("No image or git info specified for the app")
os.Exit(1)
}

if gitURL != "" {
// TODO : Add in help menu
if os.Getenv("GIT_TOKEN") == "" {
utility.Error("GIT_TOKEN env var not found")
os.Exit(1)
}
config.GitInfo = &civogo.GitInfo{}
config.GitInfo.GitToken = os.Getenv("GIT_TOKEN")
config.GitInfo.GitURL = gitURL
pullPref := &civogo.PullPreference{}
if branchName != "" {
pullPref.Branch = &branchName
}
if tagName != "" {
pullPref.Tag = &tagName
}
config.GitInfo.PullPreference = pullPref
}

if image != "" {
config.Image = &image
}

if len(args) > 0 {
if args[0] != "" {
if utility.ValidNameLength(args[0]) {
utility.Warning("the name cannot be longer than 40 characters")
os.Exit(1)
}
config.Name = args[0]
}
}

if appSize != "" {
config.Size = appSize
}

var application *civogo.Application
resp, err := client.CreateApplication(config)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

var executionTime string
if wait {
startTime := utility.StartTime()
stillCreating := true
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Prefix = fmt.Sprintf("Creating application (%s)... ", resp.Name)
s.Start()

var statusAvailable bool
for stillCreating {
application, err = client.FindApplication(resp.Name)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
if strings.ToLower(application.Status) == "available" && !statusAvailable {
statusAvailable = true
fmt.Println("Environment has been set up, deploying workloads now.")
}
if strings.ToLower(application.Status) == "ready" {
stillCreating = false
s.Stop()
} else {
time.Sleep(2 * time.Second)
}
}
executionTime = utility.TrackTime(startTime)
} else {
application, err = client.FindApplication(resp.Name)
if err != nil {
utility.Error("App %s", err)
os.Exit(1)
}
}

if common.OutputFormat == "human" {
if executionTime != "" {
fmt.Printf("The app %s has been created in %s\n", utility.Green(application.Name), executionTime)
} else {
fmt.Printf("The app %s is deploying\n", utility.Green(application.Name))
}
} else {
ow := utility.NewOutputWriter()
ow.StartLine()
ow.AppendDataWithLabel("id", resp.ID, "ID")
ow.AppendDataWithLabel("name", resp.Name, "Name")
ow.AppendDataWithLabel("network_id", resp.NetworkID, "Network ID")
ow.AppendDataWithLabel("size", resp.Size, "Size")
ow.AppendDataWithLabel("status", resp.Status, "Status")

if common.OutputFormat == "json" {
ow.WriteSingleObjectJSON(common.PrettySet)
} else {
ow.WriteCustomOutput(common.OutputFields)
}
}
},
}
Loading