Skip to content

Commit 6ba171d

Browse files
authored
Support to load default config file from hd-home (#249)
1 parent 191a976 commit 6ba171d

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

pkg/installer/process.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package installer
22

33
import (
44
"fmt"
5-
"github.com/linuxsuren/http-downloader/pkg/common"
6-
"github.com/linuxsuren/http-downloader/pkg/compress"
7-
"github.com/linuxsuren/http-downloader/pkg/exec"
85
"io"
6+
"io/ioutil"
97
"os"
108
"path"
119
"path/filepath"
1210
"runtime"
11+
12+
"github.com/linuxsuren/http-downloader/pkg/common"
13+
"github.com/linuxsuren/http-downloader/pkg/compress"
14+
"github.com/linuxsuren/http-downloader/pkg/exec"
1315
)
1416

1517
// Install installs a package
@@ -57,6 +59,28 @@ func (o *Installer) Install() (err error) {
5759
}
5860
}
5961

62+
if o.Package != nil {
63+
for i := range o.Package.DefaultConfigFile {
64+
configFile := o.Package.DefaultConfigFile[i]
65+
configFilePath := configFile.Path
66+
configDir := filepath.Dir(configFilePath)
67+
68+
if configFile.OS == runtime.GOOS {
69+
if err = os.MkdirAll(configDir, 0755); err != nil {
70+
err = fmt.Errorf("cannot create config dir: %s, error: %v", configDir, err)
71+
return
72+
}
73+
74+
if err = ioutil.WriteFile(configFilePath, []byte(configFile.Content), 0622); err != nil {
75+
err = fmt.Errorf("cannot write config file: %s, error: %v", configFilePath, err)
76+
return
77+
}
78+
79+
fmt.Printf("config file [%s] is ready.\n", configFilePath)
80+
}
81+
}
82+
}
83+
6084
if err == nil && o.Package != nil && o.Package.PostInstalls != nil {
6185
err = runCommandList(o.Package.PostInstalls)
6286
}

pkg/installer/types.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,39 @@ type PackagingFormat struct {
88

99
// HDConfig is the config of http-downloader
1010
type HDConfig struct {
11-
Name string `yaml:"name"`
12-
Categories []string `yaml:"categories"`
13-
Filename string `yaml:"filename"`
14-
FormatOverrides PackagingFormat `yaml:"formatOverrides"`
15-
Binary string `yaml:"binary"`
16-
TargetBinary string `yaml:"targetBinary"`
17-
TargetDirectory string `yaml:"targetDirectory"`
18-
AdditionBinaries []string `yaml:"additionBinaries"`
19-
FromSource bool `yaml:"fromSource"`
20-
URL string `yaml:"url"`
21-
Tar string `yaml:"tar"`
22-
LatestVersion string `yaml:"latestVersion"`
23-
SupportOS []string `yaml:"supportOS"`
24-
SupportArch []string `yaml:"supportArch"`
25-
Replacements map[string]string `yaml:"replacements"`
26-
Requirements []string `yaml:"requirements"`
27-
Installation *CmdWithArgs `yaml:"installation"`
28-
PreInstalls []CmdWithArgs `yaml:"preInstalls"`
29-
PostInstalls []CmdWithArgs `yaml:"postInstalls"`
30-
TestInstalls []CmdWithArgs `yaml:"testInstalls"`
31-
Version string `yaml:"version"`
11+
Name string `yaml:"name"`
12+
Categories []string `yaml:"categories"`
13+
Filename string `yaml:"filename"`
14+
FormatOverrides PackagingFormat `yaml:"formatOverrides"`
15+
Binary string `yaml:"binary"`
16+
TargetBinary string `yaml:"targetBinary"`
17+
TargetDirectory string `yaml:"targetDirectory"`
18+
AdditionBinaries []string `yaml:"additionBinaries"`
19+
FromSource bool `yaml:"fromSource"`
20+
URL string `yaml:"url"`
21+
Tar string `yaml:"tar"`
22+
LatestVersion string `yaml:"latestVersion"`
23+
SupportOS []string `yaml:"supportOS"`
24+
SupportArch []string `yaml:"supportArch"`
25+
Replacements map[string]string `yaml:"replacements"`
26+
Requirements []string `yaml:"requirements"`
27+
Installation *CmdWithArgs `yaml:"installation"`
28+
DefaultConfigFile []ConfigFile `yaml:"defaultConfigFiles"`
29+
PreInstalls []CmdWithArgs `yaml:"preInstalls"`
30+
PostInstalls []CmdWithArgs `yaml:"postInstalls"`
31+
TestInstalls []CmdWithArgs `yaml:"testInstalls"`
32+
Version string `yaml:"version"`
3233

3334
Org, Repo string
3435
}
3536

37+
// ConfigFile represents a config file
38+
type ConfigFile struct {
39+
OS string `yaml:"os"`
40+
Path string `yaml:"path"`
41+
Content string `yaml:"content"`
42+
}
43+
3644
// CmdWithArgs is a command with arguments
3745
type CmdWithArgs struct {
3846
Cmd string `yaml:"cmd"`

0 commit comments

Comments
 (0)