-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (177 loc) · 5.38 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// SPDX-License-Identifier: MIT
// Package main implements the core logic of running composable Dagger pipelines
// Documentation [is hosted in GitHub pages](https://9elements.github.io/firmware-action/)
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"github.com/9elements/firmware-action/action/filesystem"
"github.com/9elements/firmware-action/action/logging"
"github.com/9elements/firmware-action/action/recipes"
"github.com/alecthomas/kong"
"github.com/sethvargo/go-githubactions"
)
func main() {
logging.InitLogger(slog.LevelInfo)
if err := run(context.Background()); err != nil {
slog.Error(
"firmware-action failed",
slog.Any("error", err),
)
os.Exit(1)
}
}
const firmwareActionVersion = "v0.2.0"
// CLI (Command Line Interface) holds data from environment
var CLI struct {
JSON bool `default:"false" help:"switch to JSON stdout and stderr output"`
Indent bool `default:"false" help:"enable indentation for JSON output"`
Debug bool `default:"false" help:"increase verbosity"`
Config string `type:"path" required:"" default:"${config_file}" help:"Path to configuration file"`
Build struct {
Target string `required:"" help:"Select which target to build, use ID from configuration file"`
Recursive bool `help:"Build recursively with all dependencies and payloads"`
Interactive bool `help:"Open interactive SSH into container if build fails"`
} `cmd:"build" help:"Build a target defined in configuration file"`
GenerateConfig struct{} `cmd:"generate-config" help:"Generate empty configuration file"`
Version struct{} `cmd:"version" help:"Print version and exit"`
}
func run(ctx context.Context) error {
// Get arguments
mode, err := getInputsFromEnvironment()
if err != nil {
return err
}
if mode == "" {
// Exit on "version" or "generate-config"
return nil
}
// Properly initialize logging
level := slog.LevelInfo
if CLI.Debug {
level = slog.LevelDebug
}
logging.InitLogger(
level,
logging.WithJSON(CLI.JSON),
logging.WithIndent(CLI.Indent),
)
slog.Info(
fmt.Sprintf("Running in %s mode", mode),
slog.String("input/config", CLI.Config),
slog.String("input/target", CLI.Build.Target),
slog.Bool("input/recursive", CLI.Build.Recursive),
slog.Bool("input/interactive", CLI.Build.Interactive),
)
// Parse configuration file
var myConfig *recipes.Config
myConfig, err = recipes.ReadConfig(CLI.Config)
if err != nil {
return err
}
// Lets build stuff
_, err = recipes.Build(
ctx,
CLI.Build.Target,
CLI.Build.Recursive,
CLI.Build.Interactive,
myConfig,
recipes.Execute,
)
return err
}
func getInputsFromEnvironment() (string, error) {
// Check for GitHub
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
_, exists := os.LookupEnv("GITHUB_ACTIONS")
if exists {
return parseGithub()
}
// Check for GitLab, ... (possibly add other CIs)
// TODO
// Use command line interface
return parseCli()
}
func parseCli() (string, error) {
// Get inputs from command line options
ctx := kong.Parse(
&CLI,
kong.Description("Utility to create firmware images for several open source firmware solutions"),
kong.UsageOnError(),
kong.Vars{
"config_file": "firmware-action.json",
},
)
mode := "CLI"
switch ctx.Command() {
case "build":
// This is handled elsewhere
return mode, nil
case "generate-config":
// Check if config file exists
err := filesystem.CheckFileExists(CLI.Config)
if !errors.Is(err, os.ErrNotExist) {
// The file exists, or is directory, or some other problem
slog.Error(
fmt.Sprintf("Can't generate configuration file at: %s", CLI.Config),
slog.Any("error", err),
)
return "", err
}
// Create empty config
myConfig := recipes.Config{
Coreboot: map[string]recipes.CorebootOpts{"coreboot-example": {}},
Linux: map[string]recipes.LinuxOpts{"linux-example": {}},
Edk2: map[string]recipes.Edk2Opts{"edk2-example": {}},
FirmwareStitching: map[string]recipes.FirmwareStitchingOpts{"stitching-example": {}},
}
// Convert to JSON
jsonString, err := json.MarshalIndent(myConfig, "", " ")
if err != nil {
slog.Error(
"Unable to convert the config struct into a JSON string",
slog.String("suggestion", logging.ThisShouldNotHappenMessage),
slog.Any("error", err),
)
return "", err
}
// Write to file
slog.Info(fmt.Sprintf("Generating configuration file at: %s", CLI.Config))
if err := os.WriteFile(CLI.Config, jsonString, 0o666); err != nil {
slog.Error(
"Unable to write generated configuration into file",
slog.Any("error", err),
)
return "", err
}
return "", nil
case "version":
// Print version and exit
fmt.Println(firmwareActionVersion)
return "", nil
default:
// This should not happen
err := errors.New("unsupported command")
slog.Error(
"Supplied unsupported command",
slog.String("suggestion", logging.ThisShouldNotHappenMessage),
slog.Any("error", err),
)
return mode, err
}
}
func parseGithub() (string, error) {
// Get inputs from GitHub environment
action := githubactions.New()
regexTrue := regexp.MustCompile(`(?i)true`)
CLI.Config = action.GetInput("config")
CLI.Build.Target = action.GetInput("target")
CLI.Build.Recursive = regexTrue.MatchString(action.GetInput("recursive"))
CLI.JSON = regexTrue.MatchString(action.GetInput("json"))
return "GitHub", nil
}