Skip to content

Commit

Permalink
split out tools
Browse files Browse the repository at this point in the history
  • Loading branch information
jpicht committed Mar 11, 2022
1 parent 5ff0e95 commit 8f56717
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 147 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
azcat
/azblob
/azcat
/azls
/azput
/azrm
7 changes: 7 additions & 0 deletions Makefile
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/$@
83 changes: 83 additions & 0 deletions cmd/azblob/guess.go
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
}
44 changes: 44 additions & 0 deletions cmd/azblob/main.go
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)
}
10 changes: 10 additions & 0 deletions cmd/azcat/main.go
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())
}
10 changes: 10 additions & 0 deletions cmd/azls/main.go
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())
}
10 changes: 10 additions & 0 deletions cmd/azput/main.go
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())
}
10 changes: 10 additions & 0 deletions cmd/azrm/main.go
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())
}
33 changes: 33 additions & 0 deletions internal/client.go
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
}
19 changes: 19 additions & 0 deletions internal/log.go
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)
}
30 changes: 30 additions & 0 deletions internal/main.go
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)
}
Loading

0 comments on commit 8f56717

Please sign in to comment.