Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
add error handling

fix

"clean up"

fix tests better

add clarification

caps

update parsing

lint

fix unused error while in the midst of erroring

revive the old
  • Loading branch information
AskAlice committed Jun 1, 2021
1 parent b1a6ff5 commit f1a2e90
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 107 deletions.
2 changes: 1 addition & 1 deletion commands/accounts.go
Expand Up @@ -18,7 +18,7 @@ type AccountsListCmd struct{}

// Run executes the command
func (c *AccountsListCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
s := NewSpinner(cli, "Looking up accounts", logWriters)
s := NewSpinner("Looking up accounts", logWriters)
s.Start()

accounts, err := api.Accounts()
Expand Down
41 changes: 20 additions & 21 deletions commands/apps.go
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/rs/zerolog/log"

"github.com/alecthomas/kong"
"github.com/olekukonko/tablewriter"
"github.com/section/sectionctl/api"
)
Expand All @@ -23,7 +22,7 @@ type AppsCmd struct {
List AppsListCmd `cmd help:"List apps on Section." default:"1"`
Info AppsInfoCmd `cmd help:"Show detailed app information on Section."`
Create AppsCreateCmd `cmd help:"Create new app on Section."`
Delete AppsDeleteCmd `cmd help:"Delete an existing app on Section."`
Delete AppsDeleteCmd `cmd help:"DANGER ZONE. This deletes an existing app on Section."`
Init AppsInitCmd `cmd help:"Initialize your project for deployment."`
Stacks AppsStacksCmd `cmd help:"See the available stacks to create new apps with."`
}
Expand All @@ -46,10 +45,10 @@ func NewTable(cli *CLI, out io.Writer) (t *tablewriter.Table) {
}

// Run executes the command
func (c *AppsListCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (err error) {
func (c *AppsListCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
var aids []int
if c.AccountID == 0 {
s := NewSpinner(cli, "Looking up accounts",logWriters)
s := NewSpinner("Looking up accounts",logWriters)
s.Start()

as, err := api.Accounts()
Expand All @@ -66,7 +65,7 @@ func (c *AppsListCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (
aids = append(aids, c.AccountID)
}

s := NewSpinner(cli, "Looking up apps",logWriters)
s := NewSpinner("Looking up apps",logWriters)
s.Start()
apps := make(map[int][]api.App)
for _, id := range aids {
Expand Down Expand Up @@ -104,8 +103,8 @@ type AppsInfoCmd struct {
}

// Run executes the command
func (c *AppsInfoCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
s := NewSpinner(cli, "Looking up app info", logWriters)
func (c *AppsInfoCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
s := NewSpinner("Looking up app info", logWriters)
s.Start()

app, err := api.Application(c.AccountID, c.AppID)
Expand Down Expand Up @@ -137,10 +136,10 @@ func (c *AppsInfoCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (e
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiCyanColor})
table.SetAutoMergeCells(true)
r := [][]string{
[]string{"Domain name", dom.Name},
[]string{"Zone name", dom.ZoneName},
[]string{"CNAME", dom.CNAME},
[]string{"Mode", dom.Mode},
{"Domain name", dom.Name},
{"Zone name", dom.ZoneName},
{"CNAME", dom.CNAME},
{"Mode", dom.Mode},
}
table.AppendBulk(r)
table.Render()
Expand Down Expand Up @@ -180,12 +179,12 @@ type AppsCreateCmd struct {
AccountID int `required short:"a" help:"ID of account to create the app under"`
Hostname string `required short:"d" help:"FQDN the app can be accessed at"`
Origin string `required short:"o" help:"URL to fetch the origin"`
StackName string `required short:"s" help:"Name of stack to deploy"`
StackName string `required short:"s" help:"Name of stack to deploy. Try, for example, nodejs-basic"`
}

// Run executes the command
func (c *AppsCreateCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
s := NewSpinner(cli, fmt.Sprintf("Creating new app %s", c.Hostname),logWriters)
func (c *AppsCreateCmd) Run(logWriters *LogWriters) (err error) {
s := NewSpinner(fmt.Sprintf("Creating new app %s", c.Hostname),logWriters)
s.Start()

api.Timeout = 120 * time.Second // this specific request can take a long time
Expand Down Expand Up @@ -220,8 +219,8 @@ type AppsDeleteCmd struct {
}

// Run executes the command
func (c *AppsDeleteCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
s := NewSpinner(cli, fmt.Sprintf("Deleting app with id '%d'", c.AppID),logWriters)
func (c *AppsDeleteCmd) Run(logWriters *LogWriters) (err error) {
s := NewSpinner(fmt.Sprintf("Deleting app with id '%d'", c.AppID),logWriters)
s.Start()

api.Timeout = 120 * time.Second // this specific request can take a long time
Expand All @@ -243,12 +242,12 @@ type AppsInitCmd struct {
}

// Run executes the command
func (c *AppsInitCmd) Run(cli *CLI, ctx *kong.Context) (err error) {
func (c *AppsInitCmd) Run() (err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
switch c.StackName {
case "nodejs-basic":
err := c.InitializeNodeBasicApp(ctx, stdout, stderr)
err := c.InitializeNodeBasicApp(stdout, stderr)
if err != nil {
return fmt.Errorf("[ERROR]: init completed with error %x", err)
}
Expand All @@ -268,7 +267,7 @@ func (c *AppsInitCmd) CreatePkgJSON(stdout, stderr bytes.Buffer) (err error) {
}

// InitializeNodeBasicApp initializes a basic node app.
func (c *AppsInitCmd) InitializeNodeBasicApp(ctx *kong.Context, stdout bytes.Buffer, stderr bytes.Buffer) (err error) {
func (c *AppsInitCmd) InitializeNodeBasicApp(stdout bytes.Buffer, stderr bytes.Buffer) (err error) {
if c.Force {
log.Info().Msg(fmt.Sprintln("Removing old version of package.json"))
err = os.Remove("package.json")
Expand Down Expand Up @@ -382,8 +381,8 @@ func (c *AppsInitCmd) InitializeNodeBasicApp(ctx *kong.Context, stdout bytes.Buf
type AppsStacksCmd struct{}

// Run executes the command
func (c *AppsStacksCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
s := NewSpinner(cli, "Looking up stacks",logWriters)
func (c *AppsStacksCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
s := NewSpinner("Looking up stacks",logWriters)
s.Start()
k, err := api.Stacks()
s.Stop()
Expand Down
10 changes: 5 additions & 5 deletions commands/apps_test.go
@@ -1,8 +1,8 @@
package commands

import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -40,10 +40,10 @@ func TestCommandsAppsCreateAttemptsToValidateStackOnError(t *testing.T) {
StackName: "helloworld-1.0.0",
}

ctx := context.Background()

// Invoke
err = cmd.Run(ctx)
logWriters := LogWriters{ConsoleWriter: io.Discard,FileWriter: io.Discard,ConsoleOnly: io.Discard,CarriageReturnWriter: io.Discard}
err = cmd.Run(&logWriters)

// Test
assert.True(stackCalled)
Expand Down Expand Up @@ -110,10 +110,9 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) {
fmt.Println("server.conf creation failed")
}

ctx := context.Background()

// Invoke
err = cmd.Run(ctx)
err = cmd.Run()

// Test
if tc.isFatal {
Expand All @@ -128,3 +127,4 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) {
})
}
}

6 changes: 3 additions & 3 deletions commands/certs.go
Expand Up @@ -20,9 +20,9 @@ type CertsRenewCmd struct {
}

// Run executes the command
func (c *CertsRenewCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
func (c *CertsRenewCmd) Run(ctx *kong.Context,logWriters *LogWriters) (err error) {
var aid int
s := NewSpinner(cli, "Looking up accounts",logWriters)
s := NewSpinner("Looking up accounts",logWriters)
s.Start()

as, err := api.Accounts()
Expand All @@ -49,7 +49,7 @@ func (c *CertsRenewCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters)
return fmt.Errorf("unable to find the domain '%s' under any of your accounts.\n\nTry running `sectionctl domains` to see all your domains", c.Hostname)
}

s = NewSpinner(cli, fmt.Sprintf("Renewing cert for %s", c.Hostname),logWriters)
s = NewSpinner(fmt.Sprintf("Renewing cert for %s", c.Hostname),logWriters)
s.Start()

resp, err := api.DomainsRenewCert(aid, c.Hostname)
Expand Down
24 changes: 18 additions & 6 deletions commands/deploy.go
Expand Up @@ -51,7 +51,7 @@ type PayloadValue struct {
}

// Run deploys an app to Section's edge
func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (err error) {
func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {

dir := c.Directory
if dir == "." {
Expand All @@ -65,6 +65,9 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
log.Debug().Msg(fmt.Sprintf("[WARN] %s is not a file", packageJSONPath))
}else{
packageJSONContents, err := ioutil.ReadFile(packageJSONPath)
if err != nil {
log.Info().Err(err).Msg("Error reading your package.json")
}
packageJSON ,err:= ParsePackageJSON(string(packageJSONContents))
if err != nil {
log.Info().Err(err).Msg("Error parsing package.json")
Expand All @@ -88,6 +91,9 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
if c.Environment == "Production" && len(packageJSON.Section.Environment) > 0 {
c.Environment = packageJSON.Section.Environment
}
if c.AppPath == "nodejs" && len(packageJSON.Section.ModuleName) > 0 {
c.AppPath = packageJSON.Section.ModuleName
}
if(c.AccountID == 0 || c.AppID == 0){
packageJSONExample := PackageJSON{}
packageJSONExample.Dependencies = map[string]string{"serve":"^11.3.2"}
Expand All @@ -101,7 +107,10 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
}
log.Error().Msg("You must set an accountId and appId in the flags of this command.\nPlease run the following: \n sectionctl deploy --help \n\n======OR======\nIn the package.json, add a \"section\" property. For example: ")
log.Info().RawJSON("example",[]byte(exampleStr))
ctx.PrintUsage(false)
err = ctx.PrintUsage(false)
if err != nil{
log.Error().Err(err).Msg("Errored while trying to error")
}
os.Exit(1);
}
log.Info().Msg(Green("Deploying your node.js package named %s to Account ID: %d, App ID: %d, Environment %s",packageName,c.AccountID, c.AppID, c.Environment))
Expand All @@ -118,7 +127,7 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
}
}

s := NewSpinner(cli, fmt.Sprintf("Packaging app in: %s", dir), logWriters)
s := NewSpinner(fmt.Sprintf("Packaging app in: %s", dir), logWriters)
s.Start()

ignores := []string{".lint", ".git"}
Expand Down Expand Up @@ -177,8 +186,8 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
log.Debug().Str("URL",req.URL.String())

artifactSizeMB := stat.Size() / 1024 / 1024
log.Debug().Msg(fmt.Sprintf("[DEBUG] Upload artifact is %dMB (%d bytes) large", artifactSizeMB, stat.Size()))
s = NewSpinner(cli, fmt.Sprintf("Uploading app (%dMB)...", artifactSizeMB),logWriters)
log.Debug().Msg(fmt.Sprintf("Upload artifact is %dMB (%d bytes) large", artifactSizeMB, stat.Size()))
s = NewSpinner(fmt.Sprintf("Uploading app (%dMB)...", artifactSizeMB),logWriters)
s.Start()
client := &http.Client{
Timeout: c.Timeout,
Expand All @@ -199,8 +208,11 @@ func (c *DeployCmd) Run(cli *CLI, ctx *kong.Context, logWriters *LogWriters) (er
return fmt.Errorf("failed to decode response %v", err)
}

err = globalGitService.UpdateGitViaGit(cli, ctx, c, response, logWriters)
err = globalGitService.UpdateGitViaGit(ctx, c, response, logWriters)
if err != nil {
if err.Error() == "file not found" {
return fmt.Errorf("this application is not configured to host a node.js app on Section, or, possibly, you didn't specify the proper --AppPath")
}
return fmt.Errorf("failed to trigger app update: %v", err)
}

Expand Down
12 changes: 6 additions & 6 deletions commands/deploy_test.go
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
Expand All @@ -15,15 +14,17 @@ import (
"strconv"
"testing"

"github.com/alecthomas/kong"
"github.com/section/sectionctl/api"
"github.com/stretchr/testify/assert"
)


type MockGitService struct {
Called bool
}

func (g *MockGitService) UpdateGitViaGit(ctx context.Context, c *DeployCmd, response UploadResponse) error {
func (g *MockGitService) UpdateGitViaGit(ctx *kong.Context, c *DeployCmd,response UploadResponse,logWriters *LogWriters) error {
g.Called = true
return nil
}
Expand Down Expand Up @@ -258,9 +259,6 @@ func TestCommandsDeployUploadsTarball(t *testing.T) {

mockGit := MockGitService{}
globalGitService = &mockGit

ctx := context.Background()

// Invoke
c := DeployCmd{
Directory: dir,
Expand All @@ -270,7 +268,9 @@ func TestCommandsDeployUploadsTarball(t *testing.T) {
Environment: "dev",
AppPath: "nodejs",
}
err = c.Run(ctx)
kongContext := kong.Context{}
logWriters := LogWriters{ConsoleWriter: io.Discard,FileWriter: io.Discard,ConsoleOnly: io.Discard,CarriageReturnWriter: io.Discard}
err = c.Run(&kongContext, &logWriters)

// Test
assert.NoError(err)
Expand Down
7 changes: 3 additions & 4 deletions commands/domains.go
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strconv"

"github.com/alecthomas/kong"
"github.com/olekukonko/tablewriter"
"github.com/section/sectionctl/api"
)
Expand All @@ -21,10 +20,10 @@ type DomainsListCmd struct {
}

// Run executes the command
func (c *DomainsListCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters) (err error) {
func (c *DomainsListCmd) Run(cli *CLI, logWriters *LogWriters) (err error) {
var aids []int
if c.AccountID == 0 {
s := NewSpinner(cli, "Looking up accounts",logWriters)
s := NewSpinner("Looking up accounts",logWriters)
s.Start()

as, err := api.Accounts()
Expand All @@ -40,7 +39,7 @@ func (c *DomainsListCmd) Run(cli *CLI, ctx *kong.Context,logWriters *LogWriters)
aids = append(aids, c.AccountID)
}

s := NewSpinner(cli, "Looking up domains",logWriters)
s := NewSpinner("Looking up domains",logWriters)
s.Start()
domains := make(map[int][]api.DomainsResponse)
for _, id := range aids {
Expand Down
12 changes: 8 additions & 4 deletions commands/gitService.go
Expand Up @@ -19,7 +19,7 @@ import (

// GitService interface provides a way to interact with Git
type GitService interface {
UpdateGitViaGit(cli *CLI, ctx *kong.Context, c *DeployCmd, response UploadResponse, logWriters *LogWriters) error
UpdateGitViaGit(ctx *kong.Context, c *DeployCmd, response UploadResponse, logWriters *LogWriters) error
}

// GS ...
Expand All @@ -29,7 +29,7 @@ type GS struct{}
var globalGitService GitService = &GS{}

// UpdateGitViaGit clones the application repository to a temporary directory then updates it with the latest payload id and pushes a new commit
func (g *GS) UpdateGitViaGit(cli *CLI, ctx *kong.Context, c *DeployCmd, response UploadResponse,logWriters *LogWriters) error {
func (g *GS) UpdateGitViaGit(ctx *kong.Context, c *DeployCmd, response UploadResponse,logWriters *LogWriters) error {
app, err := api.Application(c.AccountID, c.AppID)
if err != nil {
return err
Expand Down Expand Up @@ -173,12 +173,16 @@ func (g *GS) UpdateGitViaGit(cli *CLI, ctx *kong.Context, c *DeployCmd, response
}
}
if moduleVersion == "unknown"{
log.Debug().Msg(fmt.Sprintln("failed to pair app path (aka proxy name) with image (version)"))
log.Debug().Msg("failed to pair app path (aka proxy name) with image (version)")
}
// for proxy, _ := range sectionConfig["proxychain"]{

// }
log.Info().Str("Git Remote",cloneDir).Str("Module Name",c.AppPath).Str("Module Version",moduleVersion).Str("Tarball Source",fmt.Sprintf("%v/%s.tar.gz",c.AccountID,response.PayloadID)).Msg("Pushing...")
log.Info().Str("Git Remote",cloneDir).Msg("")
log.Info().Str("Tarball Source",fmt.Sprintf("%v/%s.tar.gz",c.AccountID,response.PayloadID)).Msg("")
log.Info().Str("Module Name",c.AppPath).Msg("")
log.Info().Str("Module Version",moduleVersion).Msg("")
log.Info().Msg("Validating your app...")
err = r.Push(&git.PushOptions{Auth: gitAuth, Progress: progressOutput})

if err != nil {
Expand Down

0 comments on commit f1a2e90

Please sign in to comment.