Added support for global config files
This commit is contained in:
parent
51b7e0569c
commit
582eaf6e48
5
go.mod
5
go.mod
@ -4,4 +4,7 @@ go 1.20
|
|||||||
|
|
||||||
require github.com/arran4/golang-ical v0.0.0-20230213232137-07c6aad5e4f0
|
require github.com/arran4/golang-ical v0.0.0-20230213232137-07c6aad5e4f0
|
||||||
|
|
||||||
require github.com/BurntSushi/toml v1.2.1
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.2.1
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
|||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
38
main.go
38
main.go
@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -12,6 +14,7 @@ import (
|
|||||||
"github.com/NickyBoy89/assign-notify/selection"
|
"github.com/NickyBoy89/assign-notify/selection"
|
||||||
"github.com/NickyBoy89/assign-notify/timeline"
|
"github.com/NickyBoy89/assign-notify/timeline"
|
||||||
ics "github.com/arran4/golang-ical"
|
ics "github.com/arran4/golang-ical"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ConstructCalendarUrl(
|
func ConstructCalendarUrl(
|
||||||
@ -29,7 +32,11 @@ func ConstructCalendarUrl(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ConfigFileName = "config.toml"
|
// ConfigFileLocations is a list of every place that the config file could be located
|
||||||
|
// The items are explored in-order, and tildes are expanded to the user's home directory
|
||||||
|
var ConfigFileLocations = []string{
|
||||||
|
"~/.config/assign-notify/config.toml",
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MoodleRoot string `toml:"moodle_root"`
|
MoodleRoot string `toml:"moodle_root"`
|
||||||
@ -39,14 +46,37 @@ type Config struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Load the config
|
// Load the config
|
||||||
|
var configFileLocation string
|
||||||
|
for _, path := range ConfigFileLocations {
|
||||||
|
location, err := homedir.Expand(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to get home directory: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(location); err == nil {
|
||||||
|
configFileLocation = location
|
||||||
|
} else {
|
||||||
|
// If there actually was an error, and the file wasn't just missing
|
||||||
|
// Otherwise, check the next file in the list
|
||||||
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
|
log.Fatalf("Error accessing config file at %s: %s", location, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No configuration was found
|
||||||
|
if configFileLocation == "" {
|
||||||
|
log.Fatalf("No configuration file found, looked in: %s", ConfigFileLocations)
|
||||||
|
}
|
||||||
|
|
||||||
var conf Config
|
var conf Config
|
||||||
confFile, err := os.Open(ConfigFileName)
|
confFile, err := os.Open(configFileLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to open config %s: %s", ConfigFileName, err)
|
log.Fatalf("Unable to open config %s: %s", configFileLocation, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := toml.NewDecoder(confFile).Decode(&conf); err != nil {
|
if _, err := toml.NewDecoder(confFile).Decode(&conf); err != nil {
|
||||||
log.Fatalf("Error decoding %s: %s", ConfigFileName, err)
|
log.Fatalf("Error decoding %s: %s", configFileLocation, err)
|
||||||
}
|
}
|
||||||
confFile.Close()
|
confFile.Close()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user