-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
261 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
azcat | ||
/azblob | ||
/azcat | ||
/azls | ||
/azput | ||
/azrm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ALL=$(shell ls cmd) | ||
SOURCES=$(shell find pkg internal -name \*.go) | ||
|
||
all: ${ALL} | ||
|
||
${ALL}: cmd/$@ ${SOURCES} | ||
go build ./cmd/$@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
) | ||
|
||
func getExplicitMode() azcat.Mode { | ||
modes := []azcat.Mode{} | ||
|
||
if *read { | ||
modes = append(modes, azcat.EMode.List()) | ||
} | ||
|
||
if *list { | ||
modes = append(modes, azcat.EMode.List()) | ||
} | ||
|
||
if *remove { | ||
modes = append(modes, azcat.EMode.Remove()) | ||
} | ||
|
||
if *write { | ||
modes = append(modes, azcat.EMode.Write()) | ||
} | ||
|
||
if len(modes) == 0 { | ||
return azcat.EMode.None() | ||
} | ||
|
||
if len(modes) == 1 { | ||
return modes[0] | ||
} | ||
|
||
log.Fatal("list, remove and write are mutually exclusive") | ||
|
||
panic("unreachable") | ||
} | ||
|
||
func guessMode(blobUrl azblob.BlobURLParts, serviceClient *azblob.ServiceClient) azcat.Mode { | ||
if blobUrl.BlobName == "/" || blobUrl.BlobName == "" { | ||
return azcat.EMode.List() | ||
} | ||
|
||
blobClient := serviceClient.NewContainerClient(blobUrl.ContainerName).NewBlobClient(blobUrl.BlobName) | ||
_, err := blobClient.GetProperties(context.TODO(), &azblob.GetBlobPropertiesOptions{}) | ||
|
||
var ( | ||
exists = true | ||
stdInIsPipe = isPipe(os.Stdin) | ||
) | ||
|
||
if err != nil { | ||
if strings.Contains(err.Error(), "BlobNotFound") { | ||
exists = false | ||
} else { | ||
log.WithError(err).Fatal("GetProperties request failed") | ||
} | ||
} | ||
|
||
if !exists { | ||
return azcat.EMode.Write() | ||
} | ||
|
||
if !stdInIsPipe { | ||
return azcat.EMode.Read() | ||
} | ||
|
||
return azcat.EMode.None() | ||
} | ||
|
||
func isPipe(f *os.File) bool { | ||
s, err := f.Stat() | ||
if err != nil { | ||
log.WithError(err).Fatal("cannot stat STDIN") | ||
} | ||
|
||
return s.Mode()&os.ModeCharDevice == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
"github.com/jpicht/azcat/internal" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
log = logrus.StandardLogger().WithField("module", "main") | ||
|
||
debug = pflag.BoolP("debug", "d", false, "Enable debug output") | ||
list = pflag.BoolP("list", "l", false, "Enable list mode") | ||
read = pflag.BoolP("read", "r", false, "Enable read mode") | ||
remove = pflag.BoolP("remove", "x", false, "Enable remove mode") | ||
write = pflag.BoolP("write", "w", false, "Enable write mode") | ||
) | ||
|
||
func main() { | ||
pflag.Parse() | ||
|
||
if *debug { | ||
logrus.StandardLogger().SetLevel(logrus.DebugLevel) | ||
} | ||
|
||
mode := getExplicitMode() | ||
|
||
parsed, err := azblob.NewBlobURLParts(pflag.Arg(0)) | ||
|
||
if err != nil { | ||
log.WithError(err).Fatalf("Invalid URL %#v", pflag.Arg(0)) | ||
return | ||
} | ||
|
||
serviceClient := internal.GetClient(parsed) | ||
|
||
if mode == azcat.EMode.None() { | ||
mode = guessMode(parsed, serviceClient) | ||
} | ||
|
||
azcat.Run(mode, parsed, serviceClient) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jpicht/azcat/internal" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
) | ||
|
||
func main() { | ||
internal.Main(azcat.EMode.Read()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jpicht/azcat/internal" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
) | ||
|
||
func main() { | ||
internal.Main(azcat.EMode.List()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jpicht/azcat/internal" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
) | ||
|
||
func main() { | ||
internal.Main(azcat.EMode.Write()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jpicht/azcat/internal" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
) | ||
|
||
func main() { | ||
internal.Main(azcat.EMode.Remove()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
"github.com/jpicht/azcat/pkg/auth" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func GetClient(parsed azblob.BlobURLParts) *azblob.ServiceClient { | ||
clientBuilder := auth.AuthFromEnv() | ||
if clientBuilder == nil { | ||
log.Fatal("No client credentials could be detected") | ||
return nil | ||
} | ||
|
||
service := (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host}).String() | ||
|
||
log.WithFields(logrus.Fields{ | ||
"service_url": service, | ||
"container": parsed.ContainerName, | ||
"blob": parsed.BlobName, | ||
}).Debug() | ||
|
||
serviceClient, err := clientBuilder.CreateClient(service) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Cannot create service client") | ||
return nil | ||
} | ||
|
||
return &serviceClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
log = GetLog("internal") | ||
|
||
debug = pflag.BoolP("debug", "d", false, "Enable debug output") | ||
) | ||
|
||
func GetLog(module string) logrus.FieldLogger { | ||
if debug != nil && *debug { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
} | ||
return logrus.StandardLogger().WithField("module", module) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" | ||
"github.com/jpicht/azcat/pkg/azcat" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
func Main(mode azcat.Mode) { | ||
pflag.Parse() | ||
|
||
if *debug { | ||
logrus.StandardLogger().SetLevel(logrus.DebugLevel) | ||
} | ||
|
||
if pflag.NArg() != 1 { | ||
log.Fatal("Invalid number of arguments") | ||
} | ||
|
||
parsed, err := azblob.NewBlobURLParts(pflag.Arg(0)) | ||
|
||
if err != nil { | ||
log.WithError(err).WithField("url", pflag.Arg(0)).Fatalf("invalid URL") | ||
} | ||
|
||
serviceClient := GetClient(parsed) | ||
|
||
azcat.Run(mode, parsed, serviceClient) | ||
} |
Oops, something went wrong.