Skip to content

Commit eec6fc1

Browse files
author
R0n0066
committed
fix (config): Embed config files to golang binary
1 parent 6827a54 commit eec6fc1

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

.goreleaser.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ builds:
88
- linux
99
goarch:
1010
- amd64
11-
archive:
12-
files:
13-
- default.yaml
14-
- jira.yaml
11+
ldflags:
12+
- -a -installsuffix

cmd/cmd-packr.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/jira.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
package cmd
2222

2323
import (
24+
"bytes"
2425
"fmt"
2526

27+
"github.com/gobuffalo/packr"
2628
"github.com/spf13/cobra"
2729
"github.com/spf13/viper"
2830
)
@@ -40,10 +42,10 @@ var versionCmd = &cobra.Command{
4042
}
4143

4244
func jiraPreRun(cmd *cobra.Command, args []string) {
43-
viper.AddConfigPath("./")
45+
box := packr.NewBox("../configs")
46+
s := box.String("jira.yaml")
4447
viper.SetConfigType("yaml")
45-
viper.SetConfigName("jira")
46-
err := viper.ReadInConfig()
48+
err := viper.ReadConfig(bytes.NewBuffer([]byte(s)))
4749
checkErr(err)
4850

4951
promptList()

cmd/root.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
package cmd
2222

2323
import (
24+
"bytes"
2425
"os"
2526

27+
"github.com/gobuffalo/packr"
2628
homedir "github.com/mitchellh/go-homedir"
2729
"github.com/spf13/cobra"
2830
"github.com/spf13/viper"
@@ -73,18 +75,18 @@ func init() {
7375
}
7476

7577
func loadLocalConfigFile(name string) error {
76-
viper.AddConfigPath("./")
78+
box := packr.NewBox("../configs")
79+
s := box.String(name + ".yaml")
7780
viper.SetConfigType("yaml")
78-
viper.SetConfigName(name)
79-
return viper.ReadInConfig()
81+
return viper.ReadConfig(bytes.NewBuffer([]byte(s)))
8082
}
8183

8284
// initConfig reads in config file and ENV variables if set.
8385
func initConfig() {
8486
err := loadLocalConfigFile("default")
8587
checkErr(err)
8688

87-
projectDir, err := os.Executable()
89+
projectDir, err := os.Getwd()
8890
checkErr(err)
8991
projectConfigFile := projectDir + "/.cmf.yaml"
9092

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)