Skip to content

Commit

Permalink
fix azblob not seeing --debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jpicht committed Mar 16, 2022
1 parent b9d10de commit 11c5fdf
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 49 deletions.
4 changes: 2 additions & 2 deletions actions/log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package actions

import "github.com/sirupsen/logrus"
import "github.com/jpicht/azcat/internal"

var (
log = logrus.StandardLogger().WithField("module", "azcat")
log = internal.GetLog("actions")
)
33 changes: 33 additions & 0 deletions actions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package actions

import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/jpicht/azcat/internal"
"github.com/spf13/pflag"
)

func Main(mode Mode) {
pflag.Parse()

log := internal.GetLog("internal.Main")

if pflag.NArg() != 1 {
log.Fatal("Invalid number of arguments")
}

raw := pflag.Arg(0)

if raw == "" {
log.Fatal("Azure URL cannot be empty")
}

parsed, err := azblob.NewBlobURLParts(raw)

if err != nil {
log.WithError(err).WithField("url", raw).Fatalf("invalid URL")
}

serviceClient := internal.GetClient(parsed)

Run(mode, parsed, serviceClient)
}
2 changes: 1 addition & 1 deletion cmd/azblob/guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getExplicitMode() actions.Mode {
return modes[0]
}

log.Fatal("list, remove and write are mutually exclusive")
log.Fatal("list, ping, read, remove and write are mutually exclusive")

panic("unreachable")
}
Expand Down
17 changes: 14 additions & 3 deletions cmd/azblob/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

var (
log = internal.GetLog("azblob")
log logrus.FieldLogger

list = pflag.BoolP("list", "l", false, "Enable list mode")
ping = pflag.BoolP("ping", "p", false, "Enable ping mode")
Expand All @@ -19,20 +20,30 @@ var (

func main() {
pflag.Parse()
log = internal.GetLog("azblob")

mode := getExplicitMode()

parsed, err := azblob.NewBlobURLParts(pflag.Arg(0))
log.WithField("mode", mode).Debug()

raw := pflag.Arg(0)

if raw == "" {
log.Fatal("Azure URL cannot be empty")
}

parsed, err := azblob.NewBlobURLParts(raw)

if err != nil {
log.WithError(err).Fatalf("Invalid URL %#v", pflag.Arg(0))
log.WithError(err).Fatalf("Invalid URL %#v", raw)
return
}

serviceClient := internal.GetClient(parsed)

if mode == actions.EMode.None() {
mode = guessMode(parsed, serviceClient)
log.WithField("mode", mode).Debug("guessMode")
}

actions.Run(mode, parsed, serviceClient)
Expand Down
3 changes: 1 addition & 2 deletions cmd/multi/azcat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
)

func main() {
internal.Main(actions.EMode.Read())
actions.Main(actions.EMode.Read())
}
3 changes: 1 addition & 2 deletions cmd/multi/azls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
)

func main() {
internal.Main(actions.EMode.List())
actions.Main(actions.EMode.List())
}
3 changes: 1 addition & 2 deletions cmd/multi/azping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
)

func main() {
internal.Main(actions.EMode.Ping())
actions.Main(actions.EMode.Ping())
}
3 changes: 1 addition & 2 deletions cmd/multi/azput/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
)

func main() {
internal.Main(actions.EMode.Write())
actions.Main(actions.EMode.Write())
}
3 changes: 1 addition & 2 deletions cmd/multi/azrm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"github.com/jpicht/azcat/actions"
"github.com/jpicht/azcat/internal"
)

func main() {
internal.Main(actions.EMode.Remove())
actions.Main(actions.EMode.Remove())
}
1 change: 1 addition & 0 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func GetClient(parsed azblob.BlobURLParts) *azblob.ServiceClient {
log := GetLog("internal.GetClient")
clientBuilder := auth.AuthFromEnv()
if clientBuilder == nil {
log.Fatal("No client credentials could be detected")
Expand Down
9 changes: 6 additions & 3 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
)

var (
log = GetLog("internal")

debug = pflag.BoolP("debug", "d", false, "Enable debug output")
)

func GetLog(module string) logrus.FieldLogger {
if debug != nil && *debug {
if debug == nil {
pflag.Parse()
}

if *debug {
logrus.SetLevel(logrus.DebugLevel)
}

return logrus.StandardLogger().WithField("module", module)
}
30 changes: 0 additions & 30 deletions internal/main.go

This file was deleted.

0 comments on commit 11c5fdf

Please sign in to comment.