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
- name: Checkout code
uses: actions/checkout@v2
- name: Bundle init scripts
run: |
go get -u github.com/kevinburke/go-bindata/...
go generate
- name: Golang CI
uses: golangci/golangci-lint-action@v2
with:

View file

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

View file

@ -68,16 +68,7 @@ New: &new{},
## Test your functionality
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:
Even with unit tests, it's a good idea to build and validate the changes:
```shell
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
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

View file

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