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

Optimization Portion Pull Requestion #25

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
777Denoiser marked this conversation as resolved.
Show resolved Hide resolved
dist/
grabit
grabit.lock
Expand Down
9 changes: 6 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func addAdd(cmd *cobra.Command) {
addCmd.Flags().String("algo", internal.RecommendedAlgo, "Integrity algorithm")
addCmd.Flags().String("filename", "", "Target file name to use when downloading the resource")
addCmd.Flags().StringArray("tag", []string{}, "Resource tags")
addCmd.Flags().Bool("dynamic", false, "Mark the resource as dynamic (skip integrity checks)")
cmd.AddCommand(addCmd)
}

Expand All @@ -42,13 +43,15 @@ func runAdd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
err = lock.AddResource(args, algo, tags, filename)
dynamic, err := cmd.Flags().GetBool("dynamic")
if err != nil {
return err
}
err = lock.Save()

err = lock.AddResource(args, algo, tags, filename, dynamic)
if err != nil {
return err
}
return nil

return lock.Save()
}
27 changes: 26 additions & 1 deletion cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package cmd

import (
"github.com/cisco-open/grabit/downloader"
"github.com/cisco-open/grabit/internal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -19,10 +22,20 @@ func addDownload(cmd *cobra.Command) {
downloadCmd.Flags().StringArray("tag", []string{}, "Only download the resources with the given tag")
downloadCmd.Flags().StringArray("notag", []string{}, "Only download the resources without the given tag")
downloadCmd.Flags().String("perm", "", "Optional permissions for the downloaded files (e.g. '644')")
downloadCmd.Flags().BoolP("verbose", "v", false, "Enable verbose output")
cmd.AddCommand(downloadCmd)
}

func runFetch(cmd *cobra.Command, args []string) error {
logLevel, _ := cmd.Flags().GetString("log-level")
level, _ := zerolog.ParseLevel(logLevel)
zerolog.SetGlobalLevel(level)

if level <= zerolog.DebugLevel {
log.Debug().Msg("Starting download")
// Add more debug logs as needed
}

lockFile, err := cmd.Flags().GetString("lock-file")
if err != nil {
return err
Expand All @@ -47,9 +60,21 @@ func runFetch(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
err = lock.Download(dir, tags, notags, perm)

d := cmd.Context().Value("downloader").(*downloader.Downloader)

if verbose {
log.Debug().Str("lockFile", lockFile).Str("dir", dir).Strs("tags", tags).Strs("notags", notags).Str("perm", perm).Msg("Starting download")
}

err = lock.Download(dir, tags, notags, perm, d)
if err != nil {
return err
}

if verbose {
log.Debug().Msg("Download completed successfully")
}

return nil
}
File renamed without changes.
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cmd

import (
"context"
"os"
"path/filepath"
"strings"
Expand All @@ -13,6 +14,8 @@ import (
"github.com/spf13/cobra"
)

var verbose bool

func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "grabit",
Expand All @@ -21,10 +24,14 @@ func NewRootCmd() *cobra.Command {
}
cmd.PersistentFlags().StringP("lock-file", "f", filepath.Join(getPwd(), GRAB_LOCK), "lockfile path (default: $PWD/grabit.lock")
cmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)")
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")

addDelete(cmd)
addDownload(cmd)
addAdd(cmd)
addVersion(cmd)
AddUpdate(cmd)
AddVerify(cmd)
return cmd
}

Expand Down Expand Up @@ -59,6 +66,11 @@ func initLog(ll string) {
}

func Execute(rootCmd *cobra.Command) {
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
ctx := context.WithValue(cmd.Context(), "downloader", d)
cmd.SetContext(ctx)
}

ll, err := rootCmd.PersistentFlags().GetString("log-level")
if err != nil {
log.Fatal().Msg(err.Error())
Expand Down
3 changes: 3 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"bytes"
"github.com/cisco-open/grabit/downloader"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand All @@ -11,6 +13,7 @@ func TestRunRoot(t *testing.T) {
rootCmd := NewRootCmd()
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
d := downloader.NewDownloader(10 * time.Second) // Create a new downloader with a 10-second timeout
Execute(rootCmd)
assert.Contains(t, buf.String(), "and verifies their integrity")
}
24 changes: 24 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/cisco-open/grabit/internal"
"github.com/spf13/cobra"
)

var updateCmd = &cobra.Command{
Use: "update [URL]",
Short: "Update a resource",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
lockFile, _ := cmd.Flags().GetString("lock-file")
lock, err := internal.NewLock(lockFile, false)
if err != nil {
return err
}
return lock.UpdateResource(args[0])
},
}

func AddUpdate(cmd *cobra.Command) {
cmd.AddCommand(updateCmd)
}
23 changes: 23 additions & 0 deletions cmd/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/cisco-open/grabit/internal"
"github.com/spf13/cobra"
)

var verifyCmd = &cobra.Command{
Use: "verify",
Short: "Verify the integrity of downloaded resources",
RunE: func(cmd *cobra.Command, args []string) error {
lockFile, _ := cmd.Flags().GetString("lock-file")
lock, err := internal.NewLock(lockFile, false)
if err != nil {
return err
}
return lock.VerifyIntegrity()
},
}

func AddVerify(cmd *cobra.Command) {
cmd.AddCommand(verifyCmd)
}
Loading
Loading