Skip to content
Merged

Dev #10

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
22 changes: 0 additions & 22 deletions Makefile

This file was deleted.

12 changes: 11 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ tasks:
dev:
desc: 👨‍💻 Watch mode (requires reflex or air)
cmds:
- reflex -r '\.go$$' -s -- sh -c "task build && task run"
- reflex -r '\.go$$' -s -- sh -c "task build && task run"

go-fmt:
desc: 🧹 Cleaning all go code
cmds:
- gofumpt -l -w .

go-lint:
desc: 🚀 Command for linting code
cmds:
- golangci-lint run ./...
22 changes: 10 additions & 12 deletions cmd/cli/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/apiqube/cli/internal/core/manifests"
"github.com/apiqube/cli/internal/core/manifests/loader"
"github.com/apiqube/cli/internal/core/store"
"github.com/apiqube/cli/ui"
"github.com/apiqube/cli/ui/cli"
"github.com/spf13/cobra"
)

Expand All @@ -20,39 +20,37 @@ var Cmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
file, err := cmd.Flags().GetString("file")
if err != nil {
ui.Errorf("Failed to parse --file: %s", err.Error())
cli.Errorf("Failed to parse --file: %s", err.Error())
return
}

ui.Printf("Loading manifests from: %s", file)
ui.Spinner(true, "Applying manifests...")
defer ui.Spinner(false)
cli.Infof("Loading manifests from: %s", file)

loadedMans, cachedMans, err := loader.LoadManifests(file)
if err != nil {
ui.Errorf("Failed to load manifests: %s", err.Error())
cli.Errorf("Failed to load manifests: %s", err.Error())
return
}

printManifestsLoadResult(loadedMans, cachedMans)

if err := store.Save(loadedMans...); err != nil {
ui.Error("Failed to save manifests: " + err.Error())
cli.Infof("Failed to save manifests: %s", err.Error())
return
}

ui.Println("Manifests applied successfully")
cli.Success("Manifests applied successfully")
},
}

func printManifestsLoadResult(newMans, cachedMans []manifests.Manifest) {
ui.Infof("Loaded %d new manifests", len(newMans))

for _, m := range newMans {
ui.Infof("New manifest added: %s (h: %s...)", m.GetID(), ui.ShortHash(m.GetMeta().GetHash()))
cli.Infof("New manifest added: %s (h: %s...)", m.GetID(), cli.ShortHash(m.GetMeta().GetHash()))
}

for _, m := range cachedMans {
ui.Infof("Manifest %s unchanged (h: %s...) - using cached version", m.GetID(), ui.ShortHash(m.GetMeta().GetHash()))
cli.Infof("Manifest %s unchanged (h: %s...) - using cached version", m.GetID(), cli.ShortHash(m.GetMeta().GetHash()))
}

cli.Infof("Loaded new manifests\nNew: %d\nCached: %d", len(newMans), len(cachedMans))
}
12 changes: 5 additions & 7 deletions cmd/cli/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"strings"

"github.com/apiqube/cli/ui/cli"

"github.com/apiqube/cli/internal/core/manifests"
"github.com/apiqube/cli/internal/core/manifests/kinds/plan"
"github.com/apiqube/cli/internal/core/manifests/loader"
runner "github.com/apiqube/cli/internal/core/runner/plan"
"github.com/apiqube/cli/internal/core/store"
"github.com/apiqube/cli/ui"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,9 +40,6 @@ var cmdPlanCheck = &cobra.Command{
return uiErrorf("%s", err.Error())
}

ui.Spinner(true, "Checking manifests...")
defer ui.Spinner(false)

loadedManifests, err := loadManifests(opts)
if err != nil {
return uiErrorf("Failed to load manifests: %v", err)
Expand All @@ -56,7 +54,7 @@ var cmdPlanCheck = &cobra.Command{
return uiErrorf("Failed to check plan: %v", err)
}

ui.Successf("Successfully checked plan manifest")
cli.Successf("Successfully checked plan manifest")
return nil
},
}
Expand Down Expand Up @@ -100,7 +98,7 @@ type (
)

func uiErrorf(format string, args ...interface{}) error {
ui.Errorf(format, args...)
cli.Errorf(format, args...)
return nil
}

Expand All @@ -124,7 +122,7 @@ func loadManifests(opts *checkPlanOptions) ([]manifests.Manifest, error) {
case opts.flagsSet["file"]:
loadedMans, _, err := loader.LoadManifests(opts.file)
if err == nil {
ui.Infof("Manifests from provided path %s loaded", opts.file)
cli.Infof("Manifests from provided path %s loaded", opts.file)
}
return loadedMans, err

Expand Down
10 changes: 4 additions & 6 deletions cmd/cli/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cleanup
import (
"fmt"

"github.com/apiqube/cli/ui/cli"

"github.com/apiqube/cli/internal/core/store"
"github.com/apiqube/cli/ui"
"github.com/spf13/cobra"
)

Expand All @@ -29,14 +30,11 @@ var Cmd = &cobra.Command{
keep = keepVersionDefault
}

ui.Spinner(true, "Cleaning up...")
defer ui.Spinner(false)

if err = store.CleanupOldVersions(opts.manifestID, keep); err != nil {
ui.Errorf("Failed to cleanup old versions: %v", err)
cli.Errorf("Failed to cleanup old versions: %v", err)
}

ui.Successf("Successfully cleaned up %v to last %d versions", opts.manifestID, keep)
cli.Successf("Successfully cleaned up %v to last %d versions", opts.manifestID, keep)
return nil
},
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/cli/rollback/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package rollback
import (
"fmt"

"github.com/apiqube/cli/ui/cli"

"github.com/apiqube/cli/internal/core/store"
"github.com/apiqube/cli/ui"
"github.com/spf13/cobra"
)

Expand All @@ -25,15 +26,12 @@ var Cmd = &cobra.Command{
targetVersion = 1
}

ui.Spinner(true, "Rolling back")
defer ui.Spinner(false)

if err = store.Rollback(opts.manifestID, targetVersion); err != nil {
ui.Errorf("Error rolling back to previous version: %s", err)
cli.Errorf("Error rolling back to previous version: %s", err)
return
}

ui.Successf("Successfully rolled back %s to version %d\n", opts.manifestID, targetVersion)
cli.Successf("Successfully rolled back %s to version %d\n", opts.manifestID, targetVersion)
},
}

Expand Down
24 changes: 19 additions & 5 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package cli

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/apiqube/cli/ui/cli"

"github.com/apiqube/cli/cmd/cli/apply"
"github.com/apiqube/cli/cmd/cli/check"
Expand All @@ -22,14 +26,13 @@ var configKey contextKey = "config"
var rootCmd = &cobra.Command{
Use: "qube",
Short: "ApiQube is a powerful test manager for APIs",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cfg, err := config.InitConfig()
if err != nil {
return fmt.Errorf("config init failed: %w", err)
cli.Errorf("Error initializing config: %s", err.Error())
}

cmd.SetContext(context.WithValue(cmd.Context(), configKey, cfg))
return nil
cmd.SetContext(configureContext(context.WithValue(cmd.Context(), configKey, cfg)))
},
}

Expand All @@ -45,3 +48,14 @@ func Execute() {

cobra.CheckErr(rootCmd.Execute())
}

func configureContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
<-sigCh
cancel()
}()
return ctx
}
34 changes: 15 additions & 19 deletions cmd/cli/search/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/apiqube/cli/internal/core/manifests"
"github.com/apiqube/cli/ui"
"github.com/apiqube/cli/ui/cli"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -82,45 +81,43 @@ func displayResults(manifests []manifests.Manifest) {
headers := []string{
"#",
"Hash",
"kind",
"name",
"namespace",
"version",
"Kind",
"Name",
"Namespace",
"Version",
"Created",
"Updated",
"Last Updated",
"Last Applied",
}

var rows [][]string
for i, m := range manifests {
meta := m.GetMeta()
row := []string{
fmt.Sprint(i + 1),
ui.ShortHash(meta.GetHash()),
cli.ShortHash(meta.GetHash()),
m.GetKind(),
m.GetName(),
m.GetNamespace(),
fmt.Sprint(meta.GetVersion()),
meta.GetCreatedAt().Format(time.RFC3339),
meta.GetUpdatedAt().Format(time.RFC3339),
meta.GetLastApplied().Format(time.RFC3339),
meta.GetCreatedAt().Format("2006-01-02 15:04:05"),
meta.GetUpdatedAt().Format("2006-01-02 15:04:05"),
meta.GetLastApplied().Format("2006-01-02 15:04:05"),
}
rows = append(rows, row)
}

ui.Table(headers, rows)
cli.Table(headers, rows)
}

func handleSearchResults(manifests []manifests.Manifest, opts *Options) error {
ui.Infof("Found %d manifests", len(manifests))
cli.Success("Search completed")
cli.Infof("Found %d manifests", len(manifests))

if len(opts.sortBy) > 0 {
sortManifests(manifests, opts.sortBy)
}

ui.Spinner(true, "Preparing results...")
defer ui.Spinner(false)

if opts.output {
if err := outputResults(manifests, opts); err != nil {
return fmt.Errorf("output failed: %w", err)
Expand All @@ -129,7 +126,6 @@ func handleSearchResults(manifests []manifests.Manifest, opts *Options) error {
displayResults(manifests)
}

ui.Success("Search completed")
return nil
}

Expand Down Expand Up @@ -206,7 +202,7 @@ func ensureOutputDirectory(path string) error {
}

if _, err := os.Stat(path); os.IsNotExist(err) {
ui.Infof("Creating output directory: %s", path)
cli.Infof("Creating output directory: %s", path)
if err = os.MkdirAll(path, 0o755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
Expand All @@ -221,7 +217,7 @@ func writeSeparateOutputs(manifests []manifests.Manifest, opts *Options) error {
return fmt.Errorf("failed to write manifest %s: %w", m.GetID(), err)
}
}
ui.Successf("Successfully wrote %d manifests to %s", len(manifests), opts.outputPath)
cli.Successf("Successfully wrote %d manifests to %s", len(manifests), opts.outputPath)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package search
import (
"fmt"

"github.com/apiqube/cli/ui"
"github.com/apiqube/cli/ui/cli"
"github.com/spf13/cobra"
)

Expand All @@ -30,7 +30,7 @@ time ranges, and output formatting`,
}

if len(manifests) == 0 {
ui.Warning("No manifests found matching the criteria")
cli.Warning("No manifests found matching the criteria")
return nil
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/qube/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package main

import (
"time"

"github.com/apiqube/cli/cmd/cli"
"github.com/apiqube/cli/internal/core/store"
"github.com/apiqube/cli/ui"
uicli "github.com/apiqube/cli/ui/cli"
)

func main() {
ui.Init()
defer ui.Stop()
uicli.Init()
defer uicli.Stop()

store.Init()
defer store.Stop()

cli.Execute()

time.Sleep(time.Second)
}
Loading
Loading