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 all 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
41 changes: 36 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ package cmd

import (
"fmt"
"os"

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

var (
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 @@ -35,12 +42,36 @@ of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}

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.
if err := cfg.BindFlags(rootCmd.PersistentFlags()); err != nil {
logger.Fatal("error while declaring/binding flags: %v", err)
}
}

func initConfig() {
if cfgFile == "" {
return
}
viper.SetConfigFile(cfgFile)
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
logger.Fatal("error while reading the config: %v", err)
}
err := viper.Unmarshal(&configObj, viper.DecodeHook(cfg.DecodeHook()), func(decoderConfig *mapstructure.DecoderConfig) {
// By default, viper supports mapstructure tags for unmarshalling. Override that to support yaml tag.
decoderConfig.TagName = "yaml"
},
)
if err != nil {
logger.Fatal("error while unmarshalling the config: %v", err)
}
}
Loading