Skip to content

Commit

Permalink
feat: use go embed
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Feb 27, 2021
1 parent 67f4f2d commit 404a123
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 40 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/code.yml
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/release.yml
Expand Up @@ -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: |
Expand Down
11 changes: 1 addition & 10 deletions docs/docs/contributing-segment.md
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions docs/docs/contributing-started.mdx
Expand Up @@ -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

Expand Down
34 changes: 20 additions & 14 deletions src/main.go
@@ -1,8 +1,7 @@
//go:generate go-bindata -o init.go init/

package main

import (
_ "embed"
"encoding/json"
"flag"
"fmt"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 404a123

Please sign in to comment.