Skip to content

Commit

Permalink
Implement code generation for config parsing. (#1893)
Browse files Browse the repository at this point in the history
* Create a new package called "cfg" where the generated code will reside. In future, this code will be moved into the config package to complete the migration.
* Add an import in main.go to force compile the new package. Otherwise, we'll not know of any compile errors in the codegen and generated code.
* Add a limited set of params in params.yaml to verify that the code generation works. We'll add the entire set of params in a subsequent PR.
  • Loading branch information
kislaykishore authored May 31, 2024
1 parent 5114551 commit f36979a
Show file tree
Hide file tree
Showing 11 changed files with 764 additions and 7 deletions.
97 changes: 97 additions & 0 deletions cfg/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// GENERATED CODE - DO NOT EDIT MANUALLY.

package cfg

import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type Config struct {
AppName string `yaml:"app-name"`

Debug DebugConfig `yaml:"debug"`

FileSystem FileSystemConfig `yaml:"file-system"`
}

type DebugConfig struct {
ExitOnInvariantViolation bool `yaml:"exit-on-invariant-violation"`

LogMutex bool `yaml:"log-mutex"`
}

type FileSystemConfig struct {
FileMode Octal `yaml:"file-mode"`

Uid int `yaml:"uid"`
}

func BindFlags(flagSet *pflag.FlagSet) error {
var err error

flagSet.StringP("app-name", "", "", "The application name of this mount.")

err = viper.BindPFlag("app-name", flagSet.Lookup("app-name"))
if err != nil {
return err
}

flagSet.BoolP("debug_fuse", "", true, "This flag is currently unused.")

err = flagSet.MarkDeprecated("debug_fuse", "This flag is currently unused.")
if err != nil {
return err
}

flagSet.BoolP("debug_fuse_errors", "", true, "This flag is currently unused.")

err = flagSet.MarkDeprecated("debug_fuse_errors", "This flag is currently unused.")
if err != nil {
return err
}

flagSet.BoolP("debug_invariants", "", false, "Exit when internal invariants are violated.")

err = viper.BindPFlag("debug.exit-on-invariant-violation", flagSet.Lookup("debug_invariants"))
if err != nil {
return err
}

flagSet.BoolP("debug_mutex", "", false, "Print debug messages when a mutex is held too long.")

err = viper.BindPFlag("debug.log-mutex", flagSet.Lookup("debug_mutex"))
if err != nil {
return err
}

flagSet.IntP("file-mode", "", 0, "Permissions bits for files, in octal.")

err = viper.BindPFlag("file-system.file-mode", flagSet.Lookup("file-mode"))
if err != nil {
return err
}

flagSet.IntP("uid", "", -1, "UID owner of all inodes.")

err = viper.BindPFlag("file-system.uid", flagSet.Lookup("uid"))
if err != nil {
return err
}

return nil
}
25 changes: 25 additions & 0 deletions cfg/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cfg

import (
"fmt"
)

type Octal int

func (oi Octal) String() string {
return fmt.Sprintf("%o", oi)
}
21 changes: 18 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
github.com/jacobsa/timeutil v0.0.0-20170205232429-577e5acbbcf6
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
go.opencensus.io v0.24.0
Expand Down Expand Up @@ -49,10 +51,11 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/envoyproxy/go-control-plane v0.12.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand All @@ -63,23 +66,35 @@ require (
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/xattr v0.4.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/prometheus v0.35.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240530194437-404ba88c7ed0 // indirect
google.golang.org/genproto v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
Loading

0 comments on commit f36979a

Please sign in to comment.