@@ -2,30 +2,40 @@ package desktop
22
33import (
44 "fmt"
5+ "os"
6+ "path/filepath"
57 "strconv"
68 "strings"
79
810 "github.com/AmadlaOrg/LibraryUtils/pointer"
911)
1012
13+ // IDesktop π§© Is the interface for the NewDesktopService.
1114type IDesktop interface {
12- Build (desktop * Desktop ) (string , error )
15+ Build (desktop * Desktop ) (IDesktop , error )
16+ Save (dirPath string ) error
1317}
1418
19+ // SDesktop ποΈ Is the main structure for the NewDesktopService.
1520type SDesktop struct {
21+ // π appName - Is the of the application (normally all lowercase).
22+ appName string
23+
1624 builder strings.Builder
1725}
1826
19- const (
20- defaultDesktopSectionName = "Desktop Entry"
27+ // For mocking π₯Έ.
28+ var (
29+ osMkdirAll = os .MkdirAll
30+ osWriteFile = os .WriteFile
2131)
2232
2333// Build generates the content for the `.desktop` file format
2434//
2535// Params:
2636// - π₯οΈ desktop:
2737// - π location:
28- func (service * SDesktop ) Build (desktop * Desktop ) (string , error ) {
38+ func (service * SDesktop ) Build (desktop * Desktop ) (IDesktop , error ) {
2939 var (
3040 sectionNames []string
3141 )
@@ -54,7 +64,7 @@ func (service *SDesktop) Build(desktop *Desktop) (string, error) {
5464 //
5565 err := service .processContent ("Name" , section .Names , true )
5666 if err != nil {
57- return "" , err
67+ return nil , err
5868 }
5969
6070 //
@@ -82,7 +92,7 @@ func (service *SDesktop) Build(desktop *Desktop) (string, error) {
8292 //
8393 err = service .processRequiredProperty ("X-AppVersion" , section .XAppVersion )
8494 if err != nil {
85- return "" , err
95+ return nil , err
8696 }
8797
8898 //
@@ -127,15 +137,15 @@ func (service *SDesktop) Build(desktop *Desktop) (string, error) {
127137 //
128138 err = service .processRequiredProperty ("Exec" , & section .Exec )
129139 if err != nil {
130- return "" , err
140+ return nil , err
131141 }
132142 } else if * section .Type == LinkType {
133143 //
134144 // Url
135145 //
136146 err = service .processRequiredProperty ("Url" , & section .URL )
137147 if err != nil {
138- return "" , err
148+ return nil , err
139149 }
140150 } else if * section .Type == DirectoryType {
141151 // TODO:
@@ -147,7 +157,32 @@ func (service *SDesktop) Build(desktop *Desktop) (string, error) {
147157 _ = service .processList ("MimeType" , section .MimeType , false )
148158 }
149159
150- return service .builder .String (), nil
160+ return service , nil
161+ }
162+
163+ // Save to a `.desktop` file.
164+ //
165+ // Params:
166+ // - dirPath:
167+ func (service * SDesktop ) Save (dirPath string ) error {
168+ err := osMkdirAll (dirPath , 0755 )
169+ if err != nil {
170+ return fmt .Errorf ("failed to create directory: %w" , err )
171+ }
172+
173+ // Construct the file path
174+ filePath := filepath .Join (dirPath , fmt .Sprintf ("%s.desktop" , service .appName ))
175+
176+ // Get the .desktop content
177+ desktopContent := service .builder .String ()
178+
179+ // Write to the file
180+ err = osWriteFile (filePath , []byte (desktopContent ), 0644 )
181+ if err != nil {
182+ return fmt .Errorf ("failed to write file: %w" , err )
183+ }
184+
185+ return nil
151186}
152187
153188// processContent
0 commit comments