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

Fixed Optimization Download Process and added some new features #26

Open
wants to merge 20 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
dist/
grabit
grabit.lock
Expand Down
8 changes: 5 additions & 3 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,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()
}
26 changes: 25 additions & 1 deletion cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

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

Expand All @@ -19,10 +21,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")
Copy link
Contributor

@rabadin rabadin Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding an explicit way to set the log level isn't a bad idea... but it should not be done in this PR. Let's keep this PR focused on one thing please: adding the optimization to skip the downloading if the resource is there and its integrity matches.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its already implemented and is working via a command, apologies but I feel like we will have more to show because of this during our demo, but I understand and will focus #27 on the optimization skipping of the download. and the fact that only me and dylan have something to show that is working but it's fine because its only mid-semester and we have the rest of the semester to do what is needed to be done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind i just read your other comment and will scrap this

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 +59,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go, see my comment above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, am i to completely scrap this apologies i will scrap this verbose logging.


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
2 changes: 2 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand All @@ -11,6 +12,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