Skip to content
Draft
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
20 changes: 7 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func entryPoint() int {
return 1
}

cmd, _, args := subcommands.Lookup(args)
cmd, cmdflags, _, args := subcommands.Lookup(args)
if cmd == nil {
logger.Stderr("command not found: %s\n", args[0])
return 1
Expand All @@ -396,9 +396,9 @@ func entryPoint() int {
var store storage.Store
var repo *repository.Repository

if cmd.GetFlags()&subcommands.BeforeRepositoryOpen != 0 {
if cmdflags&subcommands.BeforeRepositoryOpen != 0 {
// store and repo can stay nil
} else if cmd.GetFlags()&subcommands.BeforeRepositoryWithStorage != 0 {
} else if cmdflags&subcommands.BeforeRepositoryWithStorage != 0 {
repo, err = repository.Inexistent(ctx.GetInner(), storeConfig)
if err != nil {
logger.Stderr("%s: %s\n", flag.CommandLine.Name(), err)
Expand Down Expand Up @@ -441,12 +441,6 @@ func entryPoint() int {

ctx.StoreConfig = storeConfig

t0 := time.Now()
if err := cmd.Parse(ctx, args); err != nil {
logger.Stderr("%s: %s\n", flag.CommandLine.Name(), err)
return 1
}

c := make(chan os.Signal, 1)
go func() {
<-c
Expand All @@ -462,16 +456,16 @@ func entryPoint() int {
}
}()

var status int
t0 := time.Now()

// If we are working on a repo, rebuild the state.
if cmd.GetFlags()&subcommands.BeforeRepositoryOpen == 0 && cmd.GetFlags()&subcommands.BeforeRepositoryWithStorage == 0 {
if cmdflags&subcommands.BeforeRepositoryOpen == 0 && cmdflags&subcommands.BeforeRepositoryWithStorage == 0 {
_, err = cached.RebuildStateFromStore(ctx, repo.Configuration().RepositoryID, storeConfig, false)
if err == nil {
status, err = task.RunCommand(ctx, cmd, repo, "@agentless")
err = task.RunCommand(ctx, cmd, repo, "@agentless")
}
} else {
status, err = task.RunCommand(ctx, cmd, repo, "@agentless")
err = task.RunCommand(ctx, cmd, repo, "@agentless")
}

t1 := time.Since(t0)
Expand Down
56 changes: 25 additions & 31 deletions subcommands/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,71 +29,65 @@ import (
)

func init() {
subcommands.Register(func() subcommands.Subcommand { return &Archive{} }, 0, "archive")
subcommands.Register(Archive, 0, "archive")
}

func (cmd *Archive) Parse(ctx *appcontext.AppContext, args []string) error {
func Archive(ctx *appcontext.AppContext, repo *repository.Repository, args []string) error {
var (
rebase bool
output string
format string
)

flags := flag.NewFlagSet("archive", flag.ExitOnError)
flags.Usage = func() {
fmt.Fprintf(flags.Output(), "Usage: %s [OPTIONS] [SNAPSHOT[:PATH]]\n", flags.Name())
fmt.Fprintf(flags.Output(), "\nOPTIONS:\n")
flags.PrintDefaults()
}

flags.StringVar(&cmd.Output, "output", "", "archive pathname")
flags.BoolVar(&cmd.Rebase, "rebase", false, "strip pathname when pulling")
flags.StringVar(&cmd.Format, "format", "tarball", "archive format: tar, tarball, zip")
flags.StringVar(&output, "output", "", "archive pathname")
flags.BoolVar(&rebase, "rebase", false, "strip pathname when pulling")
flags.StringVar(&format, "format", "tarball", "archive format: tar, tarball, zip")
flags.Parse(args)

if flags.NArg() == 0 {
return fmt.Errorf("need at least one snapshot ID to pull")
}
cmd.SnapshotPrefix = flags.Arg(0)

prefix := flags.Arg(0)

supportedFormats := map[string]string{
"tar": "tar",
"tarball": "tar.gz",
"zip": "zip",
}
if _, ok := supportedFormats[cmd.Format]; !ok {
return fmt.Errorf("unsupported format %s", cmd.Format)
if _, ok := supportedFormats[format]; !ok {
return fmt.Errorf("unsupported format %s", format)
}

if cmd.Output == "" {
cmd.Output = fmt.Sprintf("plakar-%s.%s", time.Now().UTC().Format(time.RFC3339), supportedFormats[cmd.Format])
if output == "" {
output = fmt.Sprintf("plakar-%s.%s", time.Now().UTC().Format(time.RFC3339), supportedFormats[format])
}

return nil
}

type Archive struct {
subcommands.SubcommandBase

Rebase bool
Output string
Format string
SnapshotPrefix string
}

func (cmd *Archive) Execute(ctx *appcontext.AppContext, repo *repository.Repository) (int, error) {
snap, pathname, err := locate.OpenSnapshotByPath(repo, cmd.SnapshotPrefix)
snap, pathname, err := locate.OpenSnapshotByPath(repo, prefix)
if err != nil {
return 1, fmt.Errorf("archive: could not open snapshot: %s", cmd.SnapshotPrefix)
return fmt.Errorf("archive: could not open snapshot: %s", prefix)
}
defer snap.Close()

out := os.Stdout
if cmd.Output != "-" {
out, err = os.Create(cmd.Output)
if output != "-" {
out, err = os.Create(output)
if err != nil {
return 1, fmt.Errorf("failed to create %s: %w", cmd.Output, err)
return fmt.Errorf("failed to create %s: %w", output, err)
}
defer out.Close()
}

if err = snap.Archive(out, cmd.Format, []string{pathname}, cmd.Rebase); err != nil {
return 1, err
if err = snap.Archive(out, format, []string{pathname}, rebase); err != nil {
return err
}

return 0, nil
return nil
}
Loading
Loading