Strubbl / wallabago

Go wrapper for the Wallabag API
GNU General Public License v3.0
11 stars 5 forks source link

move config file handling from -stats to here #3

Closed anarcat closed 7 years ago

anarcat commented 7 years ago

this is a followup to https://github.com/Strubbl/wallabag-stats/issues/1 - the first part is obviously to add the necessary code to wallabago.

i moved the code to a separate config.go file and also included bits that were previously in token.go because it feels it makes more sense there.

this will obviously break wallabag-stats, but i will followup with a PR there soon.

in my app, the API change is minimal, i just had to do this:

                log.Printf("completed in %.2fs\n", time.Since(start).Seconds())
        }()
        flag.Parse()
-       config, err := getConfig()
+       err := wallabago.ReadConfig(*configJSON)
        if err != nil {
                log.Fatal(err.Error())
        }
-       wallabago.Config = config

looks better too!

anarcat commented 7 years ago

On 2017-01-30 11:25:57, Strubbl wrote:

In config.go, why do you have all the empty returns?

Because we have to return something. :) The variable are defined in the function declaration. For example this:

func ReadConfig(configJSON string) (err error) {
    Config, err = getConfig(configJSON)
    return
}

Declares a ReadConfig functions which takes configJSON (a string) and returns err (an error).

The return call implicitely returns the variable declared in the function definition (in this case, err).

Can't they be left out?

No, unfortunately. Go wants you to be pretty explicit sometimes.

Maybe i am missing some knowledge about returns here.

Maybe you'd like to read this section:

https://golang.org/ref/spec#Return_statements

In particular, I'm using section #3 here.

I found the spec to be quite readable. :)

A. -- Prolétaires de tous les pays, qui lave vos chaussettes?

Strubbl commented 7 years ago

Thanks for explaining and the link to the explicit chapter.

anarcat commented 7 years ago

glad to help! :) i only learned that stuff because others helped me as well :)