This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
workspace.go
71 lines (59 loc) · 1.45 KB
/
workspace.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
package util
import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"os"
)
const (
WorkspaceFilename = ".workspace.yml"
UsercacheFilename = ".user"
)
type Exercise struct {
Reference string `yaml:"reference"`
Assignment string `yaml:"assignment"`
}
type Table struct {
Reference string `yaml:"reference"`
Identifier string `yaml:"identifier"`
Name string `yaml:"name"`
}
type Workspace struct {
Title string `yaml:"title"`
Exercise Exercise `yaml:"exercise"`
//Table Table `yaml:"table"`
Corrections map[string][]string `yaml:"corrections"`
}
func ReadWorkspace() Workspace {
assertWorkspaceExists()
data, err := ioutil.ReadFile(WorkspaceFilename)
if err != nil {
log.Fatal("reading workspace failed: " + err.Error())
}
workspace := Workspace{}
err = yaml.Unmarshal(data, &workspace)
if err != nil {
log.Fatal("unmarshaling workspace faild: " + err.Error())
}
return workspace
}
func WriteUserCache(username string) {
if err := ioutil.WriteFile(UsercacheFilename, []byte(username), 0644); err != nil {
log.Fatal(err)
}
}
func ReadUserCache() string {
username, err := ioutil.ReadFile(UsercacheFilename)
if err != nil {
fmt.Fprintln(os.Stderr, Red("workspace is not initialized"))
os.Exit(1)
}
return string(username)
}
func assertWorkspaceExists() {
if _, err := os.Stat(WorkspaceFilename); os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, Red("not a ILIAS workspace"))
os.Exit(1)
}
}