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

Update example for cobra #70

Open
cristaloleg opened this issue Apr 23, 2021 · 5 comments
Open

Update example for cobra #70

cristaloleg opened this issue Apr 23, 2021 · 5 comments
Assignees
Labels
feature New feature or request

Comments

@cristaloleg
Copy link
Member

cristaloleg commented Apr 23, 2021

Current version is outdated https://play.golang.org/p/OsCR8qTCN0H

Probably there will be another repository which will contains all the examples with external dependencies for https://github.com/cristalhq libraries, with a CI on cron

@cristaloleg cristaloleg self-assigned this Apr 23, 2021
@cristaloleg cristaloleg added the feature New feature or request label May 16, 2021
@abh
Copy link

abh commented Apr 23, 2022

[update] The following example works, for a new playground link.

package main

import (
	"os"

	"github.com/cristalhq/aconfig"
	"github.com/spf13/cobra"
)

type MyConfig struct {
	User string `usage:"user of the app" default:"john"`
}

func main() {
	var cfg MyConfig
	loader := aconfig.LoaderFor(&cfg, aconfig.Config{})
	args := struct {
		logLevel string
	}{}
	cmd := &cobra.Command{
		Use: "hello",
		RunE: func(cmd *cobra.Command, _ []string) error {
			if err := loader.Load(); err != nil {
				return err
			}

			cmd.Printf("Hello World, %s!", cfg.User)
			return nil
		},
	}
	cmd.PersistentFlags().AddGoFlagSet(loader.Flags())
	cmd.PersistentFlags().StringVar(&args.logLevel, "log-level", "info", "log level: debug, info, warning")

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

@cristaloleg
Copy link
Member Author

Oh, great! Link: https://go.dev/play/p/zyXtrtL7NBu

Do you want to make a PR ? 😄 (if so, I have started adding GUIDE.md file such things, see example https://github.com/cristalhq/acmd/blob/main/GUIDE.md + link to the guide in readme.md) and if no - I will add it a bit later.

Thank you once more.

@abh
Copy link

abh commented May 29, 2022

I didn't make a PR because the thing I posted compiles and works, but outside the example it doesn't work super great having the configuration data and the command line arguments separated.

@cristaloleg
Copy link
Member Author

Can you clarify what is exactly not ok?

@abh
Copy link

abh commented Jun 4, 2022

@cristaloleg I might be missing something, but with the cobra integration the configuration gets loaded into the aconfig "system", but the parameters get loaded into the cobra parameter system.

For my use case I'd like just the aconfig system and to ignore the cobra stuff except for integration in the help text, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants