Skip to content

Commit

Permalink
fix: commit command and add logs command
Browse files Browse the repository at this point in the history
  • Loading branch information
richaardev committed Feb 2, 2024
1 parent 4c1e55f commit efe8009
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 42 deletions.
46 changes: 46 additions & 0 deletions internal/command/app/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app

import (
"fmt"

"github.com/spf13/cobra"
"github.com/squarecloudofc/cli/internal/cli"
)

func NewLogsCommand(squareCli *cli.SquareCli) *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "See your application logs",
RunE: runLogsCommand(squareCli),
}

return cmd
}

func runLogsCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string

if len(args) > 0 {
appId = args[0]
}

if len(args) < 1 {
id, err := CreateApplicationSelection(squareCli)
if err != nil {
return err
}

appId = id
}

result, err := squareCli.Rest.ApplicationLogs(appId)
if err != nil {
return err
}

fmt.Fprint(squareCli.Out(), result.Logs)

return nil
}
}
38 changes: 3 additions & 35 deletions internal/command/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package app

import (
"fmt"
"strconv"
"strings"
"text/tabwriter"

"github.com/erikgeiser/promptkit/selection"
"github.com/spf13/cobra"
Expand All @@ -25,7 +23,7 @@ func CreateApplicationSelection(squareCli *cli.SquareCli) (string, error) {
apps = append(apps, fmt.Sprintf("%s (%s)", app.Tag, app.ID))
}

sp := selection.New("What application do you want to restart?", apps)
sp := selection.New("Which application do you want to use for this action?", apps)
choice, err := sp.RunPrompt()
if err != nil {
return "", err
Expand All @@ -45,6 +43,7 @@ func NewAppCommand(squareCli *cli.SquareCli) *cobra.Command {

cmd.AddCommand(
NewDeleteCommand(squareCli),
NewLogsCommand(squareCli),
NewStartCommand(squareCli),
NewRestartCommand(squareCli),
NewStopCommand(squareCli),
Expand All @@ -56,38 +55,7 @@ func NewAppCommand(squareCli *cli.SquareCli) *cobra.Command {

func runAppCommand(squareCli *cli.SquareCli) RunEFunc {
return func(cmd *cobra.Command, args []string) (err error) {
self, err := squareCli.Rest.SelfUser()
if err != nil {
return
}

if self == nil || self.User.Tag == "" {
fmt.Fprintf(squareCli.Out(), "No user associated with current Square Cloud Token\n")
return
}

if len(self.Applications) < 1 {
fmt.Fprintln(squareCli.Out(), "You does not have any application active")
return
}

w := tabwriter.NewWriter(squareCli.Out(), 0, 0, 2, ' ', tabwriter.TabIndent)
defer w.Flush()

tags := []string{"NAME", "App ID", "MEMORY", "CLUSTER", "LANG", "WEBSITE"}
fmt.Fprintln(w, strings.Join(tags, " \t "))

for _, app := range self.Applications {
values := []string{
app.Tag,
app.ID,
strconv.Itoa(app.RAM) + "mb",
app.Cluster,
app.Lang,
strconv.FormatBool(app.IsWebsite),
}
fmt.Fprintln(w, strings.Join(values, " \t "))
}
cmd.Help()

return nil
}
Expand Down
43 changes: 36 additions & 7 deletions pkg/zipper/zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,56 @@ package zipper

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

var (
DefaultIgnoredFiles = []string{"node_modules/", ".git/", ".vscode/", ".github/", ".cache/", "package-lock.json"}
)

func shouldIgnoreFile(filedir string, file os.FileInfo) bool {
var ignoreEntries []string
ignoreEntries = append(ignoreEntries, DefaultIgnoredFiles...)

for _, entry := range ignoreEntries {
if strings.HasSuffix(entry, "/") && !file.IsDir() {
if strings.Contains(filepath.Dir(filedir)+"/", entry) {
return true
}
}

if strings.Contains(filedir, entry) {
return true
}
}

return false
}

func ZipFolder(folder string, file *os.File) error {
wd, err := os.Getwd()
if err != nil {
return err
}

w := zip.NewWriter(file)
defer w.Close()

err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
return filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if strings.Contains(path, ".git") {
absolutepath, _ := strings.CutPrefix(path, fmt.Sprintf("%s/", wd))
// r := shouldIgnoreFile(absolutepath, info)

// fmt.Printf("Should ignore %s? %t\n", absolutepath, r)
if shouldIgnoreFile(absolutepath, info) {
fmt.Println("Ignoring " + absolutepath)
return nil
}

Expand Down Expand Up @@ -53,9 +87,4 @@ func ZipFolder(folder string, file *os.File) error {

return nil
})
if err != nil {
return err
}

return nil
}

0 comments on commit efe8009

Please sign in to comment.