Skip to content

Commit e281a47

Browse files
authored
fix(lk): fail early if config file is present and invalid (#570)
1 parent 5f0b9ce commit e281a47

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

cmd/lk/agent.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ func createAgentClient(ctx context.Context, cmd *cli.Command) (context.Context,
254254
}
255255

256256
// Verify that the project and agent config match, if it exists.
257-
lkConfig, configExists, err := config.LoadTomlFile(workingDir, tomlFilename)
257+
lkConfig, configExists, err := config.LoadTOMLFile(workingDir, tomlFilename)
258258
if err != nil {
259-
fmt.Println(err.Error())
259+
return nil, err
260260
}
261261
if configExists {
262262
projectSubdomainMatch := subdomainPattern.FindStringSubmatch(project.URL)
@@ -300,7 +300,7 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
300300
}
301301

302302
logger.Debugw("Creating agent", "working-dir", workingDir)
303-
lkConfig, configExists, err := config.LoadTomlFile(workingDir, tomlFilename)
303+
lkConfig, configExists, err := config.LoadTOMLFile(workingDir, tomlFilename)
304304
if err != nil && configExists {
305305
return err
306306
}
@@ -500,8 +500,8 @@ func createAgentConfig(ctx context.Context, cmd *cli.Command) error {
500500
}
501501

502502
func deployAgent(ctx context.Context, cmd *cli.Command) error {
503-
lkConfig, configExists, err := config.LoadTomlFile(workingDir, tomlFilename)
504-
if err != nil && configExists {
503+
lkConfig, configExists, err := config.LoadTOMLFile(workingDir, tomlFilename)
504+
if err != nil {
505505
return err
506506
}
507507
if !configExists {
@@ -616,7 +616,7 @@ func getAgentStatus(ctx context.Context, cmd *cli.Command) error {
616616
}
617617

618618
func updateAgent(ctx context.Context, cmd *cli.Command) error {
619-
lkConfig, configExists, err := config.LoadTomlFile(workingDir, tomlFilename)
619+
lkConfig, configExists, err := config.LoadTOMLFile(workingDir, tomlFilename)
620620
if err != nil && configExists {
621621
return err
622622
}
@@ -901,7 +901,7 @@ func updateAgentSecrets(ctx context.Context, cmd *cli.Command) error {
901901
func getAgentName(cmd *cli.Command, agentDir string, tomlFileName string) (string, error) {
902902
agentName := cmd.String("name")
903903
if agentName == "" {
904-
lkConfig, configExists, err := config.LoadTomlFile(agentDir, tomlFileName)
904+
lkConfig, configExists, err := config.LoadTOMLFile(agentDir, tomlFileName)
905905
if err != nil && configExists {
906906
return "", err
907907
}

cmd/lk/app.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, err
147147
return ctx, nil
148148
}
149149
if project, err = loadProjectDetails(cmd); err != nil {
150+
if errors.Is(err, config.ErrInvalidConfig) {
151+
return ctx, err
152+
}
150153
if _, err = loadProjectConfig(ctx, cmd); err != nil {
151154
// something is wrong with config file
152155
return nil, err

cmd/lk/utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ func loadProjectDetails(c *cli.Command, opts ...loadOption) (*config.ProjectConf
253253
}
254254

255255
// load from config file
256-
lkConfig, _, _ := config.LoadTomlFile(workingDir, tomlFilename)
256+
lkConfig, _, err := config.LoadTOMLFile(workingDir, tomlFilename)
257+
if errors.Is(err, config.ErrInvalidConfig) {
258+
return nil, err
259+
}
257260
if lkConfig != nil {
258261
return config.LoadProjectBySubdomain(lkConfig.Project.Subdomain)
259262
}

pkg/agentfs/secrets-file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func DetectEnvFile(maybeFile string) (string, map[string]string, error) {
7979
return "", nil, nil
8080
}
8181

82-
var selectedFile string
82+
var selectedFile string = extantEnvFiles[0]
8383
if err := huh.NewForm(
8484
huh.NewGroup(
8585
huh.NewSelect[string]().

pkg/config/livekit.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package config
1717
import (
1818
"errors"
1919
"fmt"
20+
"io/fs"
2021
"os"
2122
"path/filepath"
2223

@@ -32,15 +33,19 @@ const (
3233
clientDefaults_MaxReplicas = 10
3334
)
3435

36+
var (
37+
ErrInvalidConfig = errors.New("invalid configuration file")
38+
ErrInvalidReplicaCount = fmt.Errorf("replicas cannot be greater than max_replicas: %w", ErrInvalidConfig)
39+
)
40+
3541
// Deprecated: use LiveKitTOML instead
3642
type AgentTOML struct {
3743
ProjectSubdomain string `toml:"project_subdomain"`
3844
Name string `toml:"name"`
3945
CPU CPUString `toml:"cpu"`
4046
Replicas int `toml:"replicas"`
4147
MaxReplicas int `toml:"max_replicas"`
42-
43-
Regions []string `toml:"regions"`
48+
Regions []string `toml:"regions"`
4449
}
4550

4651
type LiveKitTOML struct {
@@ -57,6 +62,7 @@ type LiveKitTOMLAgentConfig struct {
5762
CPU CPUString `toml:"cpu"`
5863
Replicas int `toml:"replicas"`
5964
MaxReplicas int `toml:"max_replicas"`
65+
Regions []string `toml:"regions"`
6066
}
6167

6268
func NewLiveKitTOML(forSubdomain string) *LiveKitTOML {
@@ -97,15 +103,17 @@ func (c *CPUString) UnmarshalTOML(v interface{}) error {
97103
return nil
98104
}
99105

100-
func LoadTomlFile(dir string, tomlFileName string) (*LiveKitTOML, bool, error) {
106+
func LoadTOMLFile(dir string, tomlFileName string) (*LiveKitTOML, bool, error) {
101107
logger.Debugw(fmt.Sprintf("loading %s file", tomlFileName))
102-
var config LiveKitTOML
108+
var config *LiveKitTOML = nil
103109
var err error
104-
var configExists bool = true
110+
var configExists bool = false
105111

106112
tomlFile := filepath.Join(dir, tomlFileName)
107113

108114
if _, err = os.Stat(tomlFile); err == nil {
115+
configExists = true
116+
109117
_, err = toml.DecodeFile(tomlFile, &config)
110118
if config.Project == nil {
111119
// Attempt to decode old agent config
@@ -125,17 +133,15 @@ func LoadTomlFile(dir string, tomlFileName string) (*LiveKitTOML, bool, error) {
125133
}
126134
}
127135
} else {
128-
if errors.Is(err, os.ErrNotExist) {
129-
configExists = false
130-
}
136+
configExists = !errors.Is(err, fs.ErrNotExist)
131137
}
132138

133139
if configExists {
134140
// validate agent config
135141
if config.HasAgent() && config.Agent.Replicas > config.Agent.MaxReplicas {
136-
return nil, configExists, fmt.Errorf("replicas cannot be greater than max_replicas")
142+
return nil, configExists, ErrInvalidReplicaCount
137143
}
138144
}
139145

140-
return &config, configExists, err
146+
return config, configExists, err
141147
}

0 commit comments

Comments
 (0)