Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal the config object #2035

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ import (
"fmt"
"os"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
err error
kislaykishore marked this conversation as resolved.
Show resolved Hide resolved
cfgFile string
configObj cfg.Config
)
var rootCmd = &cobra.Command{
Use: "gcsfuse [flags] bucket mount_point",
Short: "Mount a specified GCS bucket or all accessible buckets locally",
Expand All @@ -42,5 +50,26 @@ func Execute() {
}

func init() {
// TODO: add flags and bind them with viper here.
cobra.OnInitialize(initConfig)
kislaykishore marked this conversation as resolved.
Show resolved Hide resolved
rootCmd.PersistentFlags().StringVar(&cfgFile, "config-file", "", "Absolute path to the config file.")

// Add all the other flags.
err = cfg.BindFlags(rootCmd.PersistentFlags())
}

func initConfig() {
if err != nil || cfgFile == "" {
return
}
viper.SetConfigFile(cfgFile)
viper.SetConfigType("yaml")
err = viper.ReadInConfig()
if err != nil {
return
kislaykishore marked this conversation as resolved.
Show resolved Hide resolved
}
err = viper.Unmarshal(&configObj, viper.DecodeHook(cfg.DecodeHook()), func(decoderConfig *mapstructure.DecoderConfig) {
kislaykishore marked this conversation as resolved.
Show resolved Hide resolved
// By default, viper supports mapstructure tags for unmarshaling. Override that to support yaml tag.
decoderConfig.TagName = "yaml"
},
)
}
Loading