Skip to content

refactor: refactor envbuilder to use coder/serpent as CLI engine #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
15e00e2
Refactor options structure
BrunoQuaresma Apr 24, 2024
f723785
Merge branch 'main' of github.com:coder/envbuilder into bq/docs
BrunoQuaresma Apr 24, 2024
68d4523
Fix broken internal test
BrunoQuaresma Apr 24, 2024
1cdf3f3
Remove Dependencies struct and pass deps directly as arg
BrunoQuaresma Apr 24, 2024
ff1b0d0
Remove unecessary arg
BrunoQuaresma Apr 24, 2024
8a626ed
Migrate to user serpent
BrunoQuaresma Apr 24, 2024
247514f
Fix 'Init Args description should end with a period'
BrunoQuaresma Apr 24, 2024
c8cbdf5
Fix others 'should end with a period' errors
BrunoQuaresma Apr 24, 2024
816d8a3
Merge branch 'main' of github.com:coder/envbuilder into bq/docs
BrunoQuaresma Apr 25, 2024
e94b8c3
Fix config.json
BrunoQuaresma Apr 25, 2024
f03fa6e
Adjust description and flags
BrunoQuaresma Apr 25, 2024
9cd9367
Use options back to reduce PR changes
BrunoQuaresma Apr 25, 2024
babf6b8
Use logf back to reduce PR changes
BrunoQuaresma Apr 25, 2024
a383098
Remove Hidden
BrunoQuaresma Apr 25, 2024
f11f3f3
Document exported values on options.go
BrunoQuaresma Apr 25, 2024
e3b205b
Rename c to Options in test
BrunoQuaresma Apr 25, 2024
9b5ca70
Use shorter name for variable
BrunoQuaresma Apr 26, 2024
bb53a5a
Fix descriptions
BrunoQuaresma Apr 26, 2024
2e5aa71
Nit var declaration
BrunoQuaresma Apr 26, 2024
d67ade5
Fix git config json removal
BrunoQuaresma Apr 26, 2024
866853a
Add filesystem and logger back to Options
BrunoQuaresma Apr 26, 2024
ce4bb56
Fix WorkspaceFolder assignment
BrunoQuaresma Apr 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions cmd/envbuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/envbuilder"
"github.com/spf13/cobra"
"github.com/coder/serpent"

// *Never* remove this. Certificates are not bundled as part
// of the container, so this is necessary for all connections
Expand All @@ -23,15 +23,11 @@ import (
)

func main() {
root := &cobra.Command{
Use: "envbuilder",
// Hide usage because we don't want to show the
// "envbuilder [command] --help" output on error.
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
options := envbuilder.OptionsFromEnv(os.LookupEnv)

options := envbuilder.Options{}
cmd := serpent.Command{
Use: "envbuilder",
Options: options.CLI(),
Handler: func(inv *serpent.Invocation) error {
var sendLogs func(ctx context.Context, log ...agentsdk.Log) error
agentURL := os.Getenv("CODER_AGENT_URL")
agentToken := os.Getenv("CODER_AGENT_TOKEN")
Expand All @@ -54,7 +50,7 @@ func main() {
}
var flushAndClose func(ctx context.Context) error
sendLogs, flushAndClose = agentsdk.LogsSender(agentsdk.ExternalLogSourceID, client.PatchLogs, slog.Logger{})
defer flushAndClose(cmd.Context())
defer flushAndClose(inv.Context())

// This adds the envbuilder subsystem.
// If telemetry is enabled in a Coder deployment,
Expand All @@ -68,25 +64,26 @@ func main() {
os.Setenv("CODER_AGENT_SUBSYSTEM", subsystems)
}

options.Logger = func(level codersdk.LogLevel, format string, args ...interface{}) {
logger := func(level codersdk.LogLevel, format string, args ...interface{}) {
output := fmt.Sprintf(format, args...)
fmt.Fprintln(cmd.ErrOrStderr(), output)
fmt.Fprintln(inv.Stderr, output)
if sendLogs != nil {
sendLogs(cmd.Context(), agentsdk.Log{
sendLogs(inv.Context(), agentsdk.Log{
CreatedAt: time.Now(),
Output: output,
Level: level,
})
}
}
err := envbuilder.Run(cmd.Context(), options)

err := envbuilder.Run(inv.Context(), options, nil, logger)
if err != nil {
options.Logger(codersdk.LogLevelError, "error: %s", err)
logger(codersdk.LogLevelError, "error: %s", err)
}
return err
},
}
err := root.Execute()
err := cmd.Invoke().WithOS().Run()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
Expand Down
Loading
Loading