Skip to content

Commit

Permalink
cleanup review
Browse files Browse the repository at this point in the history
Signed-off-by: roman-kiselenko <[email protected]>
  • Loading branch information
roman-kiselenko committed Feb 27, 2024
1 parent 69288b9 commit cfd118f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cmd/limactl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func deleteAction(cmd *cobra.Command, args []string) error {
}
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
deleted, err := autostart.DeleteStartAtLoginEntry(runtime.GOOS, instName)
if err != nil {
logrus.WithError(err).Warnf("file for instance (%q) not exists", instName)
} else if err == nil && deleted {
logrus.Infof("The autostart file (%q) has been deleted", autostart.GetStartAtLoginEntryPath(runtime.GOOS, instName))
if err != nil && !errors.Is(err, os.ErrNotExist) {
logrus.WithError(err).Warnf("The autostart file for instance %q does not exist", instName)
} else if deleted {
logrus.Infof("The autostart file for instance %q could not be deleted", autostart.GetFilePath(runtime.GOOS, instName))
}
}
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
Expand Down
12 changes: 6 additions & 6 deletions cmd/limactl/start-at-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func startAtLoginCommand() *cobra.Command {
startAtLoginCommand := &cobra.Command{
Use: "start-at-login INSTANCE",
Short: "Register/Unregister the unit file for instance",
Short: "Register/Unregister an autostart file for the instance",
Args: WrapArgsError(cobra.MaximumNArgs(1)),
RunE: startAtLoginAction,
ValidArgsFunction: startAtLoginComplete,
Expand Down Expand Up @@ -51,16 +51,16 @@ func startAtLoginAction(cmd *cobra.Command, args []string) error {
}
if startAtLogin {
if err := autostart.CreateStartAtLoginEntry(runtime.GOOS, inst.Name, inst.Dir); err != nil {
logrus.WithError(err).Warnf("can't create a unit file for instance (%q)", inst.Name)
logrus.WithError(err).Warnf("Can't create an autostart file for instance %q", inst.Name)
} else {
logrus.Infof("The autostart file (%q) has been created or updated", autostart.GetStartAtLoginEntryPath(runtime.GOOS, inst.Name))
logrus.Infof("The autostart file (%q) has been created or updated", autostart.GetFilePath(runtime.GOOS, inst.Name))
}
} else {
deleted, err := autostart.DeleteStartAtLoginEntry(runtime.GOOS, instName)
if err != nil {
logrus.WithError(err).Warnf("file for instance (%q) not exists", instName)
} else if err == nil && deleted {
logrus.Infof("The autostart file (%q) has been deleted", autostart.GetStartAtLoginEntryPath(runtime.GOOS, instName))
logrus.WithError(err).Warnf("The autostart file for instance %q does not exist", instName)
} else if deleted {
logrus.Infof("The autostart file (%q) has been deleted", autostart.GetFilePath(runtime.GOOS, instName))
}
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/autostart/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var launchdTemplate string

// CreateStartAtLoginEntry respect host OS arch and create unit file
func CreateStartAtLoginEntry(hostOS, instName, workDir string) error {
unitPath := GetStartAtLoginEntryPath(hostOS, instName)
if _, err := os.Stat(unitPath); err != nil && !errors.Is(err, os.ErrNotExist) && !errors.Is(err, os.ErrExist) {
unitPath := GetFilePath(hostOS, instName)
if _, err := os.Stat(unitPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
tmpl, err := renderTemplate(hostOS, instName, workDir, os.Executable)
Expand All @@ -37,17 +37,17 @@ func CreateStartAtLoginEntry(hostOS, instName, workDir string) error {
if err := os.WriteFile(unitPath, tmpl, 0o644); err != nil {
return err
}
return enableDisableService("enable", hostOS, GetStartAtLoginEntryPath(hostOS, instName))
return enableDisableService("enable", hostOS, GetFilePath(hostOS, instName))
}

// DeleteStartAtLoginEntry respect host OS arch and delete unit file
// return true, nil if unit file has been deleted
func DeleteStartAtLoginEntry(hostOS, instName string) (bool, error) {
unitPath := GetStartAtLoginEntryPath(hostOS, instName)
unitPath := GetFilePath(hostOS, instName)
if _, err := os.Stat(unitPath); err != nil {
return false, err
}
if err := enableDisableService("disable", hostOS, GetStartAtLoginEntryPath(hostOS, instName)); err != nil {
if err := enableDisableService("disable", hostOS, GetFilePath(hostOS, instName)); err != nil {
return false, err
}
if err := os.Remove(unitPath); err != nil {
Expand All @@ -57,10 +57,10 @@ func DeleteStartAtLoginEntry(hostOS, instName string) (bool, error) {
}

// GetFilePath returns the path to autostart file with respect of host
func GetStartAtLoginEntryPath(hostOS, instName string) string {
var fileTmp string
func GetFilePath(hostOS, instName string) string {
var fileTemplate string
if hostOS == "darwin" { // launchd plist
fileTmp = fmt.Sprintf("%s/Library/LaunchAgents/io.lima-vm.autostart.%s.plist", os.Getenv("HOME"), instName)
fileTemplate = fmt.Sprintf("%s/Library/LaunchAgents/io.lima-vm.autostart.%s.plist", os.Getenv("HOME"), instName)
}
if hostOS == "linux" { // systemd service
// Use instance name as argument to systemd service
Expand All @@ -69,9 +69,9 @@ func GetStartAtLoginEntryPath(hostOS, instName string) string {
if xdgConfigHome == "" {
xdgConfigHome = filepath.Join(os.Getenv("HOME"), ".config")
}
fileTmp = fmt.Sprintf("%s/systemd/user/lima-vm@%s.service", xdgConfigHome, instName)
fileTemplate = fmt.Sprintf("%s/systemd/user/lima-vm@%s.service", xdgConfigHome, instName)
}
return fileTmp
return fileTemplate
}

func enableDisableService(action, hostOS, serviceWithPath string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/autostart/autostart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestGetFilePath(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
assert.Check(t, strings.HasSuffix(GetStartAtLoginEntryPath(tt.HostOS, tt.InstanceName), tt.Expected))
assert.Check(t, strings.HasSuffix(GetFilePath(tt.HostOS, tt.InstanceName), tt.Expected))
})
}
}

0 comments on commit cfd118f

Please sign in to comment.