aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 8c49ce37a72422dcc1dbf2522e95d7a19ebfee89 (plain)
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
package main

import (
	"errors"
	"fmt"
	"os"
)

type Conf struct {
	Source string
	Dest   string
}

func main() {
	conf, err := loadConf()
	if err != nil {
		fmt.Printf("could not load configuration: %v\n", err)
		os.Exit(1)
	}
	if len(os.Args) < 2 {
		fmt.Printf("usage: conf <adopt|apply> [files...]\n")
		os.Exit(1)
	}
	switch os.Args[1] {
	case "apply":
		if err := apply(conf, os.Args[2:]); err != nil {
			fmt.Printf("%v\n", err)
			os.Exit(1)
		}
	case "adopt":
		if err := adopt(conf, os.Args[2:]); err != nil {
			fmt.Printf("%v\n", err)
			os.Exit(1)
		}
	default:
		fmt.Printf("unrecognized command: %s\n", os.Args[1])
		os.Exit(1)
	}
}

func loadConf() (*Conf, error) {
	source := os.Getenv("CONF_SOURCE")
	info, err := os.Stat(source)
	if err != nil {
		return nil, err
	}
	if !info.IsDir() {
		return nil, errors.New("CONF_SOURCE is not a directory")
	}
	home, err := os.UserHomeDir()
	if err != nil {
		return nil, err
	}
	return &Conf{Source: source, Dest: home}, nil
}