rwx-yxu / weather

A weather bonzai branch that will gather met office weather info.
Apache License 2.0
0 stars 0 forks source link

Migrate to Cobra #5

Open rwx-yxu opened 3 months ago

rwx-yxu commented 3 months ago

Have a Cobra implementation of the weather command just to have experience with a more popular go cli framework compared to bonzai.

rwx-yxu commented 3 months ago

Tagging some cobra repo examples: https://github.com/alexellis/arkade/tree/master https://github.com/rwxrob/lab/tree/b3d097cabbe7225388b1159ce801edc1de7e9912/go/cobra/simple-go https://github.com/cli/cli/tree/trunk/pkg/cmd https://www.youtube.com/watch?v=WOLrDPsswLE

rwx-yxu commented 3 months ago

Weather uses the vars branch to have persistent storage for API Key, location and region ids. Cobra has no equivalent command package. Will have to either set them as env vars or use persistent flags.

rwx-yxu commented 3 months ago

Breakdown of the current command map:

rwx-yxu commented 3 months ago

I could use Viper to load in a env file from os.UserCacheDir() I can put this stuff in the app package? or make a new package called conf? https://dev.to/techschoolguru/load-config-from-file-environment-variables-in-golang-with-viper-2j2d

rwx-yxu commented 3 months ago

Having mixed thoughts on the project structure. Git hub gh cli groups the cmds by package and each sub command gets its own package. This does mean that you will have multiple create packages in different places for common command names. I did initially think that there would be a conflict but from go pov it would package them together even if the same package names are in different directories during compile. Not entirely sure this is a good approach either.

Option 1:

Option 2:

rwx-yxu commented 3 months ago

With option 2, the business logic is lifted to a higher level. I also put all site cmds, including its sub commands inside site.go and have the business logic live in site/ and region/.

Whereas with option 1, there is command level isolation. More directory depth.

rwx-yxu commented 3 months ago

Reviewing what I have now, lets keep it simple and go for option 2. If it gets too large, then I can look to refactor to option 1.

rwx-yxu commented 3 months ago

Or alternatively, I can still put site.go into site/ and rename to cmd.go and export Cmd. So imports to the root cmd will be site.Cmd

rwx-yxu commented 3 months ago

Actually, cobra already has a OnInitialize function to load in config file function. This effectively makes the conf package redundant.

rwx-yxu commented 3 months ago

Should be added to the root node based.

rwx-yxu commented 3 months ago

Actual implementation is still to keep the conf sub command because it's useful to have around as a way to output the conf as a command

rwx-yxu commented 2 months ago

Current command tree:

weather conf init weather conf data

weather site list weather site set weather site forecast