feat: use go embed

This commit is contained in:
Jan De Dobbeleer 2021-02-27 16:41:50 +01:00 committed by Jan De Dobbeleer
parent 67f4f2dad0
commit 404a123217
5 changed files with 21 additions and 40 deletions

View file

@ -29,10 +29,6 @@ jobs:
go-version: 1.16 go-version: 1.16
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Bundle init scripts
run: |
go get -u github.com/kevinburke/go-bindata/...
go generate
- name: Golang CI - name: Golang CI
uses: golangci/golangci-lint-action@v2 uses: golangci/golangci-lint-action@v2
with: with:

View file

@ -68,10 +68,6 @@ jobs:
go-version: 1.16 go-version: 1.16
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Bundle init scripts
run: |
go get -u github.com/kevinburke/go-bindata/...
go generate
- name: Asset name - name: Asset name
id: artifact id: artifact
run: | run: |

View file

@ -68,16 +68,7 @@ New: &new{},
## Test your functionality ## Test your functionality
Even with unit tests, it's a good idea to build and validate the changes. Even with unit tests, it's a good idea to build and validate the changes:
First, we need to package the init scripts:
```shell
go get -u github.com/kevinburke/go-bindata/...
go generate
```
Next, build the app and validate the changes:
```shell ```shell
go build -o $GOPATH/bin/oh-my-posh go build -o $GOPATH/bin/oh-my-posh

View file

@ -77,14 +77,6 @@ git clone git@github.com:<user>/oh-my-posh.git
## Running tests ## Running tests
The go source code can be found in the `src/` directory, make sure to change to that one before continuing. The go source code can be found in the `src/` directory, make sure to change to that one before continuing.
Once in the right directory, we need to pack the initialization scripts into the source code.
```bash
go generate
```
Provided the previous steps were performed correctly, you should now see a new file called `init.go`.
Do not wory about this file as it's ignored by git, we generate it every time the app gets build.
### Unit tests ### Unit tests

View file

@ -1,8 +1,7 @@
//go:generate go-bindata -o init.go init/
package main package main
import ( import (
_ "embed"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
@ -14,6 +13,18 @@ import (
// Version number of oh-my-posh // Version number of oh-my-posh
var Version = "development" var Version = "development"
//go:embed init/omp.ps1
var pwshInit string
//go:embed init/omp.fish
var fishInit string
//go:embed init/omp.bash
var bashInit string
//go:embed init/omp.zsh
var zshInit string
const ( const (
noExe = "echo \"Unable to find Oh my Posh executable\"" noExe = "echo \"Unable to find Oh my Posh executable\""
zsh = "zsh" zsh = "zsh"
@ -185,25 +196,20 @@ func printShellInit(shell, config string) string {
} }
switch shell { switch shell {
case pwsh: case pwsh:
return getShellInitScript(executable, config, "init/omp.ps1") return getShellInitScript(executable, config, pwshInit)
case zsh: case zsh:
return getShellInitScript(executable, config, "init/omp.zsh") return getShellInitScript(executable, config, zshInit)
case bash: case bash:
return getShellInitScript(executable, config, "init/omp.bash") return getShellInitScript(executable, config, bashInit)
case fish: case fish:
return getShellInitScript(executable, config, "init/omp.fish") return getShellInitScript(executable, config, fishInit)
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
} }
} }
func getShellInitScript(executable, config, script string) string { func getShellInitScript(executable, config, script string) string {
data, err := Asset(script) script = strings.ReplaceAll(script, "::OMP::", executable)
if err != nil { script = strings.ReplaceAll(script, "::CONFIG::", config)
return fmt.Sprintf("echo \"Unable to find initialization script %s\"", script) return script
}
init := string(data)
init = strings.ReplaceAll(init, "::OMP::", executable)
init = strings.ReplaceAll(init, "::CONFIG::", config)
return init
} }