Skip to content

Commit 5381938

Browse files
Refactor preconditions checks
1 parent f5943ab commit 5381938

File tree

4 files changed

+8
-28
lines changed

4 files changed

+8
-28
lines changed

commands/service_profile_init.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@ import (
3333
// InitProfile creates a new project file if it does not exist. If a profile name with the associated FQBN is specified,
3434
// it is added to the project.
3535
func (s *arduinoCoreServerImpl) InitProfile(ctx context.Context, req *rpc.InitProfileRequest) (*rpc.InitProfileResponse, error) {
36-
sketchPath := paths.New(req.GetSketchPath())
37-
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs()
38-
if err != nil {
39-
return nil, err
40-
}
41-
4236
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
43-
sk, err := sketch.New(sketchPath)
37+
sk, err := sketch.New(paths.New(req.GetSketchPath()))
4438
if err != nil {
4539
return nil, err
4640
}
41+
projectFilePath := sk.GetProjectPath()
4742

4843
if !projectFilePath.Exist() {
4944
err := projectFilePath.WriteFile([]byte("profiles: {}\n"))

commands/service_profile_lib_add.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ import (
2727

2828
// ProfileLibAdd adds a library to the specified profile or to the default profile.
2929
func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.ProfileLibAddRequest) (*rpc.ProfileLibAddResponse, error) {
30-
sketchPath := paths.New(req.GetSketchPath())
31-
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs()
32-
if err != nil {
33-
return nil, err
34-
}
35-
3630
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
37-
sk, err := sketch.New(sketchPath)
31+
sk, err := sketch.New(paths.New(req.GetSketchPath()))
3832
if err != nil {
3933
return nil, err
4034
}
35+
projectFilePath := sk.GetProjectPath()
4136

4237
// If no profile is specified, try to use the default one
4338
profileName := sk.Project.DefaultProfile

commands/service_profile_lib_remove.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,12 @@ import (
2626

2727
// ProfileLibRemove removes a library from the specified profile or from the default profile.
2828
func (s *arduinoCoreServerImpl) ProfileLibRemove(ctx context.Context, req *rpc.ProfileLibRemoveRequest) (*rpc.ProfileLibRemoveResponse, error) {
29-
sketchPath := paths.New(req.GetSketchPath())
30-
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs()
31-
if err != nil {
32-
return nil, err
33-
}
34-
3529
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
36-
sk, err := sketch.New(sketchPath)
30+
sk, err := sketch.New(paths.New(req.GetSketchPath()))
3731
if err != nil {
3832
return nil, err
3933
}
34+
projectFilePath := sk.GetProjectPath()
4035

4136
// If no profile is specified, try to use the default one
4237
profileName := sk.Project.DefaultProfile

commands/service_profile_set_default.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,12 @@ func (s *arduinoCoreServerImpl) ProfileSetDefault(ctx context.Context, req *rpc.
3030
return nil, &cmderrors.MissingProfileError{}
3131
}
3232

33-
sketchPath := paths.New(req.GetSketchPath())
34-
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs()
35-
if err != nil {
36-
return nil, err
37-
}
38-
3933
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
40-
sk, err := sketch.New(sketchPath)
34+
sk, err := sketch.New(paths.New(req.GetSketchPath()))
4135
if err != nil {
4236
return nil, err
4337
}
38+
projectFilePath := sk.GetProjectPath()
4439

4540
if _, err := sk.GetProfile(req.GetProfileName()); err != nil {
4641
return nil, err

0 commit comments

Comments
 (0)