Skip to content
Merged
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: 3 additions & 17 deletions cmd/pull.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
errutil "github.com/semaphoreci/artifact/pkg/errors"
"github.com/semaphoreci/artifact/pkg/files"
"github.com/semaphoreci/artifact/pkg/hub"
Expand Down Expand Up @@ -61,7 +60,7 @@ func NewPullJobCmd() *cobra.Command {
log.Info("Successfully pulled artifact for current job.\n")
log.Infof("* Remote source: '%s'.\n", paths.Source)
log.Infof("* Local destination: '%s'.\n", paths.Destination)
log.Infof("Pulled %d files. Total of %s\n", stats.FileCount, formatBytes(stats.TotalSize))
log.Infof("Pulled %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand Down Expand Up @@ -96,7 +95,7 @@ func NewPullWorkflowCmd() *cobra.Command {
log.Info("Successfully pulled artifact for current workflow.\n")
log.Infof("* Remote source: '%s'.\n", paths.Source)
log.Infof("* Local destination: '%s'.\n", paths.Destination)
log.Infof("Pulled %d files. Total of %s\n", stats.FileCount, formatBytes(stats.TotalSize))
log.Infof("Pulled %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand Down Expand Up @@ -131,7 +130,7 @@ func NewPullProjectCmd() *cobra.Command {
log.Info("Successfully pulled artifact for current project.\n")
log.Infof("* Remote source: '%s'.\n", paths.Source)
log.Infof("* Local destination: '%s'.\n", paths.Destination)
log.Infof("Pulled %d files. Total of %s\n", stats.FileCount, formatBytes(stats.TotalSize))
log.Infof("Pulled %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand All @@ -141,19 +140,6 @@ func NewPullProjectCmd() *cobra.Command {
return cmd
}

// formatBytes converts bytes to human readable format
func formatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}

func init() {
rootCmd.AddCommand(pullCmd)
Expand Down
11 changes: 7 additions & 4 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var pushCmd = &cobra.Command{
while the rest of the semaphore process, or after it.`,
}

func runPushForCategory(cmd *cobra.Command, args []string, resolver *files.PathResolver) (*files.ResolvedPath, error) {
func runPushForCategory(cmd *cobra.Command, args []string, resolver *files.PathResolver) (*files.ResolvedPath, *storage.PushStats, error) {
hubClient, err := hub.NewClient()
errutil.Check(err)

Expand Down Expand Up @@ -83,7 +83,7 @@ func NewPushJobCmd() *cobra.Command {
resolver, err := files.NewPathResolver(files.ResourceTypeJob, jobId)
errutil.Check(err)

paths, err := runPushForCategory(cmd, args, resolver)
paths, stats, err := runPushForCategory(cmd, args, resolver)
if err != nil {
log.Errorf("Error pushing artifact: %v\n", err)
errutil.Exit(1)
Expand All @@ -93,6 +93,7 @@ func NewPushJobCmd() *cobra.Command {
log.Info("Successfully pushed artifact for current job.\n")
log.Infof("* Local source: %s.\n", paths.Source)
log.Infof("* Remote destination: %s.\n", paths.Destination)
log.Infof("Pushed %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand All @@ -118,7 +119,7 @@ func NewPushWorkflowCmd() *cobra.Command {
resolver, err := files.NewPathResolver(files.ResourceTypeWorkflow, workflowId)
errutil.Check(err)

paths, err := runPushForCategory(cmd, args, resolver)
paths, stats, err := runPushForCategory(cmd, args, resolver)
if err != nil {
log.Errorf("Error pushing artifact: %v\n", err)
errutil.Exit(1)
Expand All @@ -128,6 +129,7 @@ func NewPushWorkflowCmd() *cobra.Command {
log.Info("Successfully pushed artifact for current workflow.\n")
log.Infof("* Local source: %s.\n", paths.Source)
log.Infof("* Remote destination: %s.\n", paths.Destination)
log.Infof("Pushed %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand All @@ -153,7 +155,7 @@ func NewPushProjectCmd() *cobra.Command {
resolver, err := files.NewPathResolver(files.ResourceTypeProject, projectId)
errutil.Check(err)

paths, err := runPushForCategory(cmd, args, resolver)
paths, stats, err := runPushForCategory(cmd, args, resolver)
if err != nil {
log.Errorf("Error pushing artifact: %v\n", err)
errutil.Exit(1)
Expand All @@ -163,6 +165,7 @@ func NewPushProjectCmd() *cobra.Command {
log.Info("Successfully pushed artifact for current project.\n")
log.Infof("* Local source: %s.\n", paths.Source)
log.Infof("* Remote destination: %s.\n", paths.Destination)
log.Infof("Pushed %d %s. Total of %s\n", stats.FileCount, pluralize(stats.FileCount, "file", "files"), formatBytes(stats.TotalSize))
},
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import "fmt"

// formatBytes converts bytes to human readable format
func formatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}

// pluralize returns singular or plural form based on count
func pluralize(count int, singular, plural string) string {
if count == 1 {
return singular
}
return plural
}
6 changes: 3 additions & 3 deletions pkg/storage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Pull(hubClient *hub.Client, resolver *files.PathResolver, options PullOptio
return nil, nil, err
}

stats, err := doPull(options.Force, artifacts, response.Urls)
stats, err := doPull(artifacts)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func buildArtifacts(signedURLs []*api.SignedURL, paths *files.ResolvedPath, forc
return artifacts, nil
}

func doPull(force bool, artifacts []*api.Artifact, signedURLs []*api.SignedURL) (*PullStats, error) {
func doPull(artifacts []*api.Artifact) (*PullStats, error) {
client := newHTTPClient()
stats := &PullStats{}

Expand All @@ -87,7 +87,7 @@ func doPull(force bool, artifacts []*api.Artifact, signedURLs []*api.SignedURL)
if err := signedURL.Follow(client, artifact); err != nil {
return nil, err
}

// Get file size after successful download
if fileInfo, err := os.Stat(artifact.LocalPath); err == nil {
stats.FileCount++
Expand Down
41 changes: 30 additions & 11 deletions pkg/storage/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type PushOptions struct {
Force bool
}

type PushStats struct {
FileCount int
TotalSize int64
}

func (o *PushOptions) RequestType() hub.GenerateSignedURLsRequestType {
if o.Force {
return hub.GenerateSignedURLsRequestPUSHFORCE
Expand All @@ -26,10 +31,10 @@ func (o *PushOptions) RequestType() hub.GenerateSignedURLsRequestType {
return hub.GenerateSignedURLsRequestPUSH
}

func Push(hubClient *hub.Client, resolver *files.PathResolver, options PushOptions) (*files.ResolvedPath, error) {
func Push(hubClient *hub.Client, resolver *files.PathResolver, options PushOptions) (*files.ResolvedPath, *PushStats, error) {
paths, err := resolver.Resolve(files.OperationPush, options.SourcePath, options.DestinationOverride)
if err != nil {
return nil, err
return nil, nil, err
}

log.Debug("Pushing...\n")
Expand All @@ -39,25 +44,25 @@ func Push(hubClient *hub.Client, resolver *files.PathResolver, options PushOptio

artifacts, err := LocateArtifacts(paths)
if err != nil {
return nil, err
return nil, nil, err
}

response, err := hubClient.GenerateSignedURLs(api.RemotePaths(artifacts), options.RequestType())
if err != nil {
return nil, err
return nil, nil, err
}

err = attachURLs(artifacts, response.Urls, options.Force)
if err != nil {
return nil, err
return nil, nil, err
}

err = doPush(options.Force, artifacts, response.Urls)
stats, err := doPush(artifacts)
if err != nil {
return nil, err
return nil, nil, err
}

return paths, nil
return paths, stats, nil
}

func LocateArtifacts(paths *files.ResolvedPath) ([]*api.Artifact, error) {
Expand Down Expand Up @@ -131,16 +136,30 @@ func attachURLs(items []*api.Artifact, signedURLs []*api.SignedURL, force bool)
return nil
}

func doPush(force bool, artifacts []*api.Artifact, signedURLs []*api.SignedURL) error {
func doPush(artifacts []*api.Artifact) (*PushStats, error) {
client := newHTTPClient()
stats := &PushStats{}

for _, artifact := range artifacts {
fileInfo, err := os.Stat(artifact.LocalPath)
if err != nil {
return nil, fmt.Errorf("failed to stat '%s': %v", artifact.LocalPath, err)
}

for _, signedURL := range artifact.URLs {
if err := signedURL.Follow(client, artifact); err != nil {
return err
return nil, err
}
}

for _, url := range artifact.URLs {
if url.Method == "PUT" {
stats.FileCount++
stats.TotalSize += fileInfo.Size()
break
}
}
}

return nil
return stats, nil
}
Loading
Loading