-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
71 lines (60 loc) · 2.24 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2022 Adam Chalkley
//
// https://github.com/atc0005/safelinks
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package main
import (
"flag"
"fmt"
"os"
)
// Updated via Makefile builds. Setting placeholder value here so that
// something resembling a version string will be provided for non-Makefile
// builds.
var version = "x.y.z"
// Application metadata for easy reference.
const (
myAppName string = "usl"
myAppURL string = "https://github.com/atc0005/safelinks"
)
// shorthandFlagSuffix is appended to short flag help text to emphasize that
// the flag is a shorthand version of a longer flag.
const shorthandFlagSuffix = " (shorthand)"
// Config represents configuration details for this application.
type Config struct {
URL string
Filename string
Verbose bool
Version bool
}
// NewConfig processes flag values and returns an application configuration.
func NewConfig() *Config {
var cfg Config
setupFlags(&cfg)
return &cfg
}
// Version emits application name, version and repo location.
func Version() string {
return fmt.Sprintf("%s %s (%s)", myAppName, version, myAppURL)
}
// usage is a custom override for the default Help text provided by the flag
// package. Here we prepend some additional metadata to the existing output.
func usage() {
fmt.Fprintln(flag.CommandLine.Output(), "\n"+Version()+"\n")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
// setupFlags processes given flag values.
func setupFlags(c *Config) {
flag.StringVar(&c.URL, "url", "", "Safe Links URL to decode")
flag.StringVar(&c.URL, "u", "", "Safe Links URL to decode"+shorthandFlagSuffix)
flag.StringVar(&c.Filename, "inputfile", "", "Path to file containing Safe Links URLs to decode")
flag.StringVar(&c.Filename, "f", "", "Path to file containing Safe Links URLs to decode"+shorthandFlagSuffix)
flag.BoolVar(&c.Verbose, "verbose", false, "Display additional information about a given Safe Links URL")
flag.BoolVar(&c.Verbose, "v", false, "Display additional information about a given Safe Links URL"+shorthandFlagSuffix)
flag.BoolVar(&c.Version, "version", false, "Display version information and immediately exit")
flag.Usage = usage
flag.Parse()
}