Skip to content

Commit 95f707a

Browse files
authored
feat(alzlibtool): offline mode for library checks (#223)
1 parent caf76ae commit 95f707a

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

cmd/alzlibtool/command/check/library.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,30 @@ var libraryCmd = cobra.Command{
2424
Args: cobra.ExactArgs(1),
2525
Run: func(cmd *cobra.Command, args []string) {
2626
az := alzlib.NewAlzLib(nil)
27-
creds, err := auth.NewToken()
27+
offline, err := cmd.Flags().GetBool("offline")
2828
if err != nil {
29-
cmd.PrintErrf("%s could not get Azure credential: %v\n", cmd.ErrPrefix(), err)
29+
cmd.PrintErrf("%s could not get offline flag: %v\n", cmd.ErrPrefix(), err)
3030
os.Exit(1)
3131
}
32-
cf, err := armpolicy.NewClientFactory("", creds, &arm.ClientOptions{
33-
ClientOptions: policy.ClientOptions{
34-
Cloud: auth.GetCloudFromEnv(),
35-
},
36-
})
37-
if err != nil {
38-
cmd.PrintErrf("%s could not create Azure policy client factory: %v\n", cmd.ErrPrefix(), err)
39-
os.Exit(1)
32+
33+
if !offline {
34+
creds, err := auth.NewToken()
35+
if err != nil {
36+
cmd.PrintErrf("%s could not get Azure credential: %v\n", cmd.ErrPrefix(), err)
37+
os.Exit(1)
38+
}
39+
cf, err := armpolicy.NewClientFactory("", creds, &arm.ClientOptions{
40+
ClientOptions: policy.ClientOptions{
41+
Cloud: auth.GetCloudFromEnv(),
42+
},
43+
})
44+
if err != nil {
45+
cmd.PrintErrf("%s could not create Azure policy client factory: %v\n", cmd.ErrPrefix(), err)
46+
os.Exit(1)
47+
}
48+
az.AddPolicyClient(cf)
4049
}
41-
az.AddPolicyClient(cf)
50+
4251
thisRef := alzlib.NewCustomLibraryReference(args[0])
4352
libs, err := thisRef.FetchWithDependencies(cmd.Context())
4453
if err != nil {
@@ -63,13 +72,19 @@ var libraryCmd = cobra.Command{
6372

6473
chk := checker.NewValidator(
6574
checks.CheckAllDefinitionsAreReferenced(az),
66-
checks.CheckAllArchitectures(az),
6775
checks.CheckLibraryMemberPath(az),
6876
checks.CheckDefaults(az),
6977
checks.CheckLibraryFileNames(args[0], &checks.CheckLibraryFileNameOptions{
7078
Fix: shouldFix,
7179
}),
7280
)
81+
82+
if !offline {
83+
chk = chk.AddChecks(
84+
checks.CheckAllArchitectures(az),
85+
)
86+
}
87+
7388
err = chk.Validate()
7489
if err != nil {
7590
cmd.PrintErrf("%s library check error: %v\n", cmd.ErrPrefix(), err)
@@ -82,4 +97,6 @@ func init() {
8297
libraryCmd.Flags().
8398
BoolP("fix", "f", false,
8499
"Whether to fix any fixable issues (currently only filename issues).")
100+
libraryCmd.Flags().
101+
Bool("offline", false, "Whether to run the checks in offline mode (no Azure calls).")
85102
}

internal/tools/checker/checker.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// Validator is a struct that holds a list of checks to be performed.
1414
type Validator struct {
1515
checks []ValidatorCheck
16+
quiet bool // whether to suppress check start/finish messages
1617
}
1718

1819
// ValidatorCheck is a struct that holds the name and function of a check to be performed.
@@ -41,6 +42,14 @@ func NewValidator(c ...ValidatorCheck) Validator {
4142
}
4243
}
4344

45+
// NewValidatorQuiet creates a new Validator with the given checks, which suppresses check start/finish messages.
46+
func NewValidatorQuiet(c ...ValidatorCheck) Validator {
47+
return Validator{
48+
checks: c,
49+
quiet: true,
50+
}
51+
}
52+
4453
// AddChecks adds additional checks to the Validator.
4554
func (v Validator) AddChecks(c ...ValidatorCheck) Validator {
4655
v.checks = append(v.checks, c...)
@@ -52,13 +61,17 @@ func (v Validator) Validate() error {
5261
var errs error
5362

5463
for _, c := range v.checks {
55-
io.WriteString(os.Stdout, "==> Starting check: "+c.name+"\n") // nolint: errcheck
64+
if !v.quiet {
65+
io.WriteString(os.Stdout, "==> Starting check: "+c.name+"\n") // nolint: errcheck
66+
}
5667

5768
if err := c.f(); err != nil {
5869
errs = multierror.Append(errs, err)
5970
}
6071

61-
io.WriteString(os.Stdout, "==> Finished check: "+c.name+"\n") // nolint: errcheck
72+
if !v.quiet {
73+
io.WriteString(os.Stdout, "==> Finished check: "+c.name+"\n") // nolint: errcheck
74+
}
6275
}
6376

6477
return errs

internal/tools/checks/checkLibraryFileNames.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
const (
23-
versionlessLibraryFileNameParts = 3
23+
versionlessLibraryFileNameParts = 3 // name.type.ext (no version segment), e.g. myRoleDef.alz_role_definition.json
2424
)
2525

2626
// libraryFileNameCheckModel is a model for checking library file names.
@@ -37,7 +37,7 @@ type libraryFileNameCheckModelProperties struct {
3737
}
3838

3939
func (m *libraryFileNameCheckModel) check(p libraryFileNameParts) error {
40-
v := checker.NewValidator(
40+
v := checker.NewValidatorQuiet(
4141
checkType(m, p),
4242
checkName(m, p),
4343
checkVersion(m, p),

0 commit comments

Comments
 (0)