Skip to content

Commit 235005d

Browse files
Support default profiles
If the project file contains only one profile, it is automatically set as default, otherwise the `--default` flag can be used. Library operations are automatically executed on the default profile.
1 parent f98bf7d commit 235005d

File tree

7 files changed

+517
-460
lines changed

7 files changed

+517
-460
lines changed

commands/service_profile_init.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func (s *arduinoCoreServerImpl) InitProfile(ctx context.Context, req *rpc.InitPr
9797
})
9898

9999
sk.Project.Profiles = append(sk.Project.Profiles, newProfile)
100+
// Set the profile as the default one if it's the only one
101+
if req.DefaultProfile || len(sk.Project.Profiles) == 1 {
102+
sk.Project.DefaultProfile = newProfile.Name
103+
}
104+
100105
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml()))
101106
if err != nil {
102107
return nil, err

commands/service_profile_lib_add.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
3232
return nil, err
3333
}
3434

35-
if req.GetProfileName() == "" {
36-
return nil, &cmderrors.MissingProfileError{}
37-
}
38-
3935
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
4036
sk, err := sketch.New(sketchPath)
4137
if err != nil {
4238
return nil, err
4339
}
4440

45-
profile, err := sk.GetProfile(req.ProfileName)
41+
// If no profile is specified, try to use the default one
42+
profileName := sk.Project.DefaultProfile
43+
if req.GetProfileName() != "" {
44+
profileName = req.GetProfileName()
45+
}
46+
if profileName == "" {
47+
return nil, &cmderrors.MissingProfileError{}
48+
}
49+
50+
profile, err := sk.GetProfile(profileName)
4651
if err != nil {
4752
return nil, err
4853
}
@@ -76,5 +81,5 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
7681
return nil, err
7782
}
7883

79-
return &rpc.ProfileLibAddResponse{LibName: req.LibName, LibVersion: libRelease.GetVersion().String()}, nil
84+
return &rpc.ProfileLibAddResponse{LibName: req.LibName, LibVersion: libRelease.GetVersion().String(), ProfileName: profileName}, nil
8085
}

commands/service_profile_lib_remove.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,22 @@ func (s *arduinoCoreServerImpl) ProfileLibRemove(ctx context.Context, req *rpc.P
3131
return nil, err
3232
}
3333

34-
if req.GetProfileName() == "" {
35-
return nil, &cmderrors.MissingProfileError{}
36-
}
37-
3834
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
3935
sk, err := sketch.New(sketchPath)
4036
if err != nil {
4137
return nil, err
4238
}
4339

44-
profile, err := sk.GetProfile(req.ProfileName)
40+
// If no profile is specified, try to use the default one
41+
profileName := sk.Project.DefaultProfile
42+
if req.GetProfileName() != "" {
43+
profileName = req.GetProfileName()
44+
}
45+
if profileName == "" {
46+
return nil, &cmderrors.MissingProfileError{}
47+
}
48+
49+
profile, err := sk.GetProfile(profileName)
4550
if err != nil {
4651
return nil, err
4752
}
@@ -56,5 +61,5 @@ func (s *arduinoCoreServerImpl) ProfileLibRemove(ctx context.Context, req *rpc.P
5661
return nil, err
5762
}
5863

59-
return &rpc.ProfileLibRemoveResponse{LibName: lib.Library, LibVersion: lib.Version.String()}, nil
64+
return &rpc.ProfileLibRemoveResponse{LibName: lib.Library, LibVersion: lib.Version.String(), ProfileName: profileName}, nil
6065
}

internal/cli/profile/init.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
31+
var defaultProfile bool
3132
initCommand := &cobra.Command{
3233
Use: "init",
3334
Short: i18n.Tr("Creates or updates the sketch project file."),
@@ -38,15 +39,16 @@ func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
3839
" " + os.Args[0] + " config init --profile Uno_profile -b arduino:avr:uno",
3940
Args: cobra.MaximumNArgs(1),
4041
Run: func(cmd *cobra.Command, args []string) {
41-
runInitCommand(cmd.Context(), args, srv)
42+
runInitCommand(cmd.Context(), args, srv, defaultProfile)
4243
},
4344
}
4445
fqbnArg.AddToCommand(initCommand, srv)
4546
profileArg.AddToCommand(initCommand, srv)
47+
initCommand.Flags().BoolVar(&defaultProfile, "default", false, i18n.Tr("Set the profile as the default one."))
4648
return initCommand
4749
}
4850

49-
func runInitCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer) {
51+
func runInitCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, defaultProfile bool) {
5052
path := ""
5153
if len(args) > 0 {
5254
path = args[0]
@@ -56,7 +58,7 @@ func runInitCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServi
5658

5759
inst := instance.CreateAndInit(ctx, srv)
5860

59-
resp, err := srv.InitProfile(ctx, &rpc.InitProfileRequest{Instance: inst, SketchPath: sketchPath.String(), ProfileName: profileArg.Get(), Fqbn: fqbnArg.String()})
61+
resp, err := srv.InitProfile(ctx, &rpc.InitProfileRequest{Instance: inst, SketchPath: sketchPath.String(), ProfileName: profileArg.Get(), Fqbn: fqbnArg.String(), DefaultProfile: defaultProfile})
6062
if err != nil {
6163
feedback.Fatal(i18n.Tr("Error initializing the project file: %v", err), feedback.ErrGeneric)
6264
}

internal/cli/profile/lib.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreSer
8989
if err != nil {
9090
feedback.Fatal(i18n.Tr("Error adding %s to the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric)
9191
}
92-
feedback.PrintResult(libAddResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: profileArg.Get()})
92+
feedback.PrintResult(libAddResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: resp.ProfileName})
9393
}
9494
}
9595

@@ -135,7 +135,7 @@ func runLibRemoveCommand(ctx context.Context, args []string, srv rpc.ArduinoCore
135135
if err != nil {
136136
feedback.Fatal(i18n.Tr("Error removing %s from the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric)
137137
}
138-
feedback.PrintResult(libRemoveResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: profileArg.Get()})
138+
feedback.PrintResult(libRemoveResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: resp.ProfileName})
139139
}
140140
}
141141

0 commit comments

Comments
 (0)