-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyprojectparser.go
55 lines (43 loc) · 1.11 KB
/
pyprojectparser.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
package poetryrun
import (
"errors"
"os"
"github.com/BurntSushi/toml"
)
type PyProjectConfig struct {
Tool struct {
Poetry struct {
Scripts map[string]string `toml:"scripts"`
} `toml:"poetry"`
} `toml:"tool"`
}
type PyProjectConfigParser struct {
}
func NewPyProjectConfigParser() PyProjectConfigParser {
return PyProjectConfigParser{}
}
// Parse returns the name of the script for Poetry to execute
// If there is no file, no script to run, or multiple scripts to run,
// Parse returns an empty string and a nil error
// If there is an error reading the file, Parse returns an error
func (p PyProjectConfigParser) Parse(filepath string) (string, error) {
file, err := os.Open(filepath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", err
}
var pyProjectConfig PyProjectConfig
_, err = toml.NewDecoder(file).Decode(&pyProjectConfig)
if err != nil {
return "", err
}
if len(pyProjectConfig.Tool.Poetry.Scripts) != 1 {
return "", nil
}
for key := range pyProjectConfig.Tool.Poetry.Scripts {
return key, nil
}
panic("should not be able to get here")
}