/
solution.go
63 lines (49 loc) · 1.51 KB
/
solution.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Peter Bjorklund. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package solution
import (
"bufio"
"fmt"
"io"
"os"
"path"
"github.com/pelletier/go-toml"
"github.com/swamp/compiler/src/file"
)
type Package struct {
Name string
Path string
}
type Settings struct {
Name string
Packages []string
}
func Load(reader io.Reader, solutionFileDirectory string) (Settings, error) {
data, dataErr := io.ReadAll(reader)
if dataErr != nil {
return Settings{}, dataErr
}
settings := Settings{}
unmarshalErr := toml.Unmarshal(data, &settings)
if unmarshalErr != nil {
return Settings{}, unmarshalErr
}
return settings, nil
}
func fileFromDirectory(rootDirectory string) string {
return path.Join(rootDirectory, "swamp.solution.toml")
}
func LoadIfExists(rootDirectory string) (Settings, error) {
tomlFilename := fileFromDirectory(rootDirectory)
if !file.HasFile(tomlFilename) {
return Settings{}, fmt.Errorf("didn't find solution file in %s", rootDirectory)
}
tomlFile, settingsFileErr := os.Open(tomlFilename)
if settingsFileErr != nil {
return Settings{}, fmt.Errorf("couldn't load solution file %s %w", tomlFilename, settingsFileErr)
}
settingsReader := bufio.NewReader(tomlFile)
return Load(settingsReader, rootDirectory)
}