Skip to content

Commit dc14f27

Browse files
committed
refactor: simplify and remove duplicate code
1 parent 69bcbfe commit dc14f27

File tree

5 files changed

+48
-83
lines changed

5 files changed

+48
-83
lines changed

cmd/build.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package cmd
22

33
import (
4-
"github.com/Norgate-AV/spc/internal/build"
4+
"fmt"
5+
6+
"github.com/Norgate-AV/spc/internal/compiler"
7+
"github.com/Norgate-AV/spc/internal/config"
8+
"github.com/Norgate-AV/spc/internal/utils"
59
"github.com/spf13/cobra"
610
)
711

@@ -14,22 +18,30 @@ var buildCmd = &cobra.Command{
1418
}
1519

1620
func runBuild(cmd *cobra.Command, args []string) error {
17-
service := build.NewBuildService()
18-
return service.Build(cmd, args)
21+
if len(args) == 0 {
22+
return fmt.Errorf("no files specified")
23+
}
1924

20-
// Resolve config
21-
// cfg, err := config.Load()
22-
// if err != nil {
23-
// return fmt.Errorf("error loading config: %w", err)
24-
// }
25+
// Load and validate configuration
26+
configLoader := config.NewLoader()
27+
cfg, err := configLoader.LoadForBuild(cmd, args)
28+
if err != nil {
29+
return err
30+
}
2531

26-
// fmt.Printf("%+v\n", cfg)
27-
// fmt.Printf("%+v\n", args)
32+
// Build compiler command arguments
33+
builder := compiler.NewCommandBuilder()
34+
cmdArgs, err := builder.BuildCommandArgs(cfg, args)
35+
if err != nil {
36+
return err
37+
}
2838

29-
// Validate input
30-
// if len(args) == 0 {
31-
// return cmd.Help()
32-
// }
39+
// Print build info if verbose mode is enabled
40+
if cfg.Verbose {
41+
series := utils.ParseTarget(cfg.Target)
42+
builder.PrintBuildInfo(cfg, series, args, cmdArgs)
43+
}
3344

34-
// return nil
45+
// Execute the compiler command
46+
return builder.ExecuteCommand(cfg.CompilerPath, cmdArgs)
3547
}

internal/compiler/compiler.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

internal/config/config.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99
"github.com/spf13/viper"
1010
)
1111

12+
// Default configuration values
13+
const (
14+
DefaultCompilerPath = "C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe"
15+
DefaultTarget = "234"
16+
DefaultSilent = false
17+
DefaultVerbose = false
18+
)
19+
1220
// Holds the configuration options for spc
1321
type Config struct {
1422
// Path to the Crestron SIMPL+ compiler
@@ -45,12 +53,12 @@ func Load() (*Config, error) {
4553
// Apply defaults if not set
4654
if cfg.CompilerPath == "" {
4755
if runtime.GOOS != "windows" {
48-
cfg.CompilerPath = "C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe"
56+
cfg.CompilerPath = DefaultCompilerPath
4957
}
5058
}
5159

5260
if cfg.Target == "" {
53-
cfg.Target = "234"
61+
cfg.Target = DefaultTarget
5462
}
5563

5664
// Validate required fields

internal/config/config_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ func TestLoad(t *testing.T) {
2121
name: "load with all defaults",
2222
setupViper: func() {
2323
viper.Reset()
24-
viper.SetDefault("compiler_path", "C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe")
25-
viper.SetDefault("target", "234")
26-
viper.SetDefault("silent", false)
27-
viper.SetDefault("verbose", false)
24+
viper.SetDefault("compiler_path", DefaultCompilerPath)
25+
viper.SetDefault("target", DefaultTarget)
26+
viper.SetDefault("silent", DefaultSilent)
27+
viper.SetDefault("verbose", DefaultVerbose)
2828
},
2929
wantConfig: &Config{
3030
CompilerPath: func() string {
31-
abs, _ := filepath.Abs("C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe")
31+
abs, _ := filepath.Abs(DefaultCompilerPath)
3232
return abs
3333
}(),
34-
Target: "234",
35-
Silent: false,
34+
Target: DefaultTarget,
35+
Silent: DefaultSilent,
3636
Verbose: false,
3737
UserFolders: nil, // Changed from []string{} to nil
3838
},

internal/config/loader.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func (l *Loader) LoadForBuild(cmd *cobra.Command, args []string) (*Config, error
3030

3131
// setupViperDefaults sets up default values for viper
3232
func (l *Loader) setupViperDefaults() {
33-
viper.SetDefault("compiler_path", "C:/Program Files (x86)/Crestron/Simpl/SPlusCC.exe")
34-
viper.SetDefault("target", "234")
35-
viper.SetDefault("silent", false)
36-
viper.SetDefault("verbose", false)
33+
viper.SetDefault("compiler_path", DefaultCompilerPath)
34+
viper.SetDefault("target", DefaultTarget)
35+
viper.SetDefault("silent", DefaultSilent)
36+
viper.SetDefault("verbose", DefaultVerbose)
3737
}
3838

3939
// loadGlobalConfig loads global configuration from APPDATA

0 commit comments

Comments
 (0)