Skip to content

Commit a929e02

Browse files
committed
Proper behaviour for 'profile create' command
1 parent 99c8c88 commit a929e02

File tree

2 files changed

+50
-54
lines changed

2 files changed

+50
-54
lines changed

commands/service_profile_create.go

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,67 @@ import (
3232
// ProfileCreate creates a new project file if it does not exist. If a profile name with the associated FQBN is specified,
3333
// it is added to the project.
3434
func (s *arduinoCoreServerImpl) ProfileCreate(ctx context.Context, req *rpc.ProfileCreateRequest) (*rpc.ProfileCreateResponse, error) {
35+
if req.GetProfileName() == "" {
36+
return nil, &cmderrors.MissingProfileError{}
37+
}
38+
if req.GetFqbn() == "" {
39+
return nil, &cmderrors.MissingFQBNError{}
40+
}
41+
3542
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
3643
sk, err := sketch.New(paths.New(req.GetSketchPath()))
3744
if err != nil {
3845
return nil, err
3946
}
40-
projectFilePath := sk.GetProjectPath()
4147

42-
if !projectFilePath.Exist() {
43-
err := projectFilePath.WriteFile([]byte("profiles: {}\n"))
44-
if err != nil {
45-
return nil, err
46-
}
48+
fqbn, err := fqbn.Parse(req.GetFqbn())
49+
if err != nil {
50+
return nil, &cmderrors.InvalidFQBNError{Cause: err}
4751
}
4852

49-
if req.GetProfileName() != "" {
50-
if req.GetFqbn() == "" {
51-
return nil, &cmderrors.MissingFQBNError{}
52-
}
53-
fqbn, err := fqbn.Parse(req.GetFqbn())
54-
if err != nil {
55-
return nil, &cmderrors.InvalidFQBNError{Cause: err}
56-
}
57-
58-
// Check that the profile name is unique
59-
if profile, _ := sk.GetProfile(req.ProfileName); profile != nil {
60-
return nil, &cmderrors.ProfileAlreadyExitsError{Profile: req.ProfileName}
61-
}
53+
// Check that the profile name is unique
54+
if profile, _ := sk.GetProfile(req.ProfileName); profile != nil {
55+
return nil, &cmderrors.ProfileAlreadyExitsError{Profile: req.ProfileName}
56+
}
6257

63-
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
64-
if err != nil {
65-
return nil, err
66-
}
67-
defer release()
68-
if pme.Dirty() {
69-
return nil, &cmderrors.InstanceNeedsReinitialization{}
70-
}
58+
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
59+
if err != nil {
60+
return nil, err
61+
}
62+
defer release()
63+
if pme.Dirty() {
64+
return nil, &cmderrors.InstanceNeedsReinitialization{}
65+
}
7166

72-
// Automatically detect the target platform if it is installed on the user's machine
73-
_, targetPlatform, _, _, _, err := pme.ResolveFQBN(fqbn)
74-
if err != nil {
75-
if targetPlatform == nil {
76-
return nil, &cmderrors.PlatformNotFoundError{
77-
Platform: fmt.Sprintf("%s:%s", fqbn.Vendor, fqbn.Architecture),
78-
Cause: errors.New(i18n.Tr("platform not installed")),
79-
}
67+
// Automatically detect the target platform if it is installed on the user's machine
68+
_, targetPlatform, _, _, _, err := pme.ResolveFQBN(fqbn)
69+
if err != nil {
70+
if targetPlatform == nil {
71+
return nil, &cmderrors.PlatformNotFoundError{
72+
Platform: fmt.Sprintf("%s:%s", fqbn.Vendor, fqbn.Architecture),
73+
Cause: errors.New(i18n.Tr("platform not installed")),
8074
}
81-
return nil, &cmderrors.InvalidFQBNError{Cause: err}
8275
}
76+
return nil, &cmderrors.InvalidFQBNError{Cause: err}
77+
}
8378

84-
newProfile := &sketch.Profile{Name: req.GetProfileName(), FQBN: req.GetFqbn()}
85-
// TODO: what to do with the PlatformIndexURL?
86-
newProfile.Platforms = append(newProfile.Platforms, &sketch.ProfilePlatformReference{
87-
Packager: targetPlatform.Platform.Package.Name,
88-
Architecture: targetPlatform.Platform.Architecture,
89-
Version: targetPlatform.Version,
90-
})
79+
newProfile := &sketch.Profile{Name: req.GetProfileName(), FQBN: req.GetFqbn()}
80+
// TODO: what to do with the PlatformIndexURL?
81+
newProfile.Platforms = append(newProfile.Platforms, &sketch.ProfilePlatformReference{
82+
Packager: targetPlatform.Platform.Package.Name,
83+
Architecture: targetPlatform.Platform.Architecture,
84+
Version: targetPlatform.Version,
85+
})
9186

92-
sk.Project.Profiles = append(sk.Project.Profiles, newProfile)
93-
if req.DefaultProfile {
94-
sk.Project.DefaultProfile = newProfile.Name
95-
}
87+
sk.Project.Profiles = append(sk.Project.Profiles, newProfile)
88+
if req.DefaultProfile {
89+
sk.Project.DefaultProfile = newProfile.Name
90+
}
9691

97-
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml()))
98-
if err != nil {
99-
return nil, err
100-
}
92+
projectFilePath := sk.GetProjectPath()
93+
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml()))
94+
if err != nil {
95+
return nil, err
10196
}
10297

10398
return &rpc.ProfileCreateResponse{}, nil

internal/cli/profile/profile_create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ func initProfileCreateCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
3333
var fqbnArg arguments.Fqbn
3434
var profileArg arguments.Profile
3535
createCommand := &cobra.Command{
36-
Use: "create",
36+
Use: "create --profile <PROFILE_NAME> --fqbn <FQBN> [flags] [<SKETCH_PATH>]",
3737
Short: i18n.Tr("Create or update a profile in the sketch project file."),
3838
Example: "" +
3939
" # " + i18n.Tr("Creates or updates the sketch project file in the current directory.") + "\n" +
40-
" " + os.Args[0] + " profile create\n" +
41-
" " + os.Args[0] + " profile create --profile uno_profile -b arduino:avr:uno",
40+
" " + os.Args[0] + " profile create -m uno -b arduino:avr:uno",
4241
Args: cobra.MaximumNArgs(1),
4342
Run: func(cmd *cobra.Command, args []string) {
4443
runProfileCreateCommand(cmd.Context(), args, srv, profileArg.Get(), fqbnArg.String(), setAsDefault)
4544
},
4645
}
4746
fqbnArg.AddToCommand(createCommand, srv)
4847
profileArg.AddToCommand(createCommand, srv)
48+
createCommand.MarkFlagRequired("profile")
49+
createCommand.MarkFlagRequired("fqbn")
4950
createCommand.Flags().BoolVar(&setAsDefault, "set-default", false, i18n.Tr("Set the profile as the default one."))
5051
return createCommand
5152
}

0 commit comments

Comments
 (0)