Skip to content

Commit

Permalink
add cmd to assets for individual run
Browse files Browse the repository at this point in the history
  • Loading branch information
ross96D committed Sep 3, 2024
1 parent 3a3621c commit 36f8a22
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
7 changes: 2 additions & 5 deletions server/user_handler/user_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ross96D/updater/server/user_handler"
"github.com/ross96D/updater/share"
"github.com/ross96D/updater/share/configuration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -34,17 +35,14 @@ func TestHandleUserAppsList(t *testing.T) {
Name: "-",
TaskSchedPath: "-",
SystemPath: "-",
Unzip: true,
},
{
Name: "asset1",
SystemPath: "path1",
Unzip: true,
},
{
Name: "asset1",
SystemPath: "path1",
Unzip: true,
},
},
},
Expand All @@ -58,14 +56,13 @@ func TestHandleUserAppsList(t *testing.T) {
Name: "--",
TaskSchedPath: "-",
SystemPath: "-",
Unzip: true,
},
},
},
},
}

require.Equal(t, len(expected), len(apps))
assert.Equal(t, len(expected), len(apps))
for i, a := range apps {
require.Equal(t, expected[i], a)
}
Expand Down
4 changes: 2 additions & 2 deletions share/app_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ func (u *appUpdater) updateAsset(v configuration.Asset) (fnCopy func() (err erro
}

func (u appUpdater) RunPostAction() error {
if u.app.PostAction == nil || u.state == failed {
if u.app.Command == nil || u.state == failed {
return nil
}
cmd := exec.Command(u.app.PostAction.Command, u.app.PostAction.Args...)
cmd := exec.Command(u.app.Command.Command, u.app.Command.Args...)
log.Info().Msg("running post action " + cmd.String())
b, err := cmd.Output()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion share/config_test_reload.cue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apps: [
},
]

post_action: {
cmd: {
command: "python"
args: ["-f", "-s"]
}
Expand Down
2 changes: 1 addition & 1 deletion share/configuration/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Application struct {

Assets []Asset `json:"assets"`

PostAction *Command `json:"post_action"`
Command *Command `json:"cmd"`

GithubRelease *GithubRelease `json:"github_release"`
}
Expand Down
9 changes: 5 additions & 4 deletions share/configuration/asset.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package configuration

type Asset struct {
Name string `json:"name"`
SystemPath string `json:"system_path"`
TaskSchedPath string `json:"service"`
Unzip bool `json:"unzip"`
Name string `json:"name"`
SystemPath string `json:"system_path"`
TaskSchedPath string `json:"service"`
Unzip bool `json:"unzip"`
Command *Command `json:"cmd"`
}
3 changes: 2 additions & 1 deletion share/configuration/definitions.cue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ base_path?: string // path where the temporal files used by the app will place
assets!: [...#Asset]

// use this to set a command to be run after succesfully update
post_action?: #Command
cmd?: #Command

github_release?: #GithubRelease
}
Expand All @@ -38,6 +38,7 @@ base_path?: string // path where the temporal files used by the app will place

// if this is set to true, the asset will be decompressed
unzip: bool | *false
cmd?: #Command
}

#Command: {
Expand Down
19 changes: 10 additions & 9 deletions share/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -95,10 +96,9 @@ func TestReload(t *testing.T) {
Name: "some asset name",
TaskSchedPath: "/is/a/path",
SystemPath: "/is/a/path",
Unzip: true,
},
},
PostAction: &configuration.Command{
Command: &configuration.Command{
Command: "python",
Args: []string{"-f", "-s"},
},
Expand Down Expand Up @@ -157,23 +157,24 @@ func TestConfigPathValidationWindows(t *testing.T) {

func TestPostActionCommand(t *testing.T) {
app := configuration.Application{
PostAction: &configuration.Command{
Command: &configuration.Command{
Command: "echo",
Args: []string{"-n", "test"},
},
}

err := share.NewAppUpdater(app, share.NoData{}).RunPostAction()

require.Equal(t, nil, err)
require.NoError(t, err)
}

func TestUnzip(t *testing.T) {
t.Run("zip ext", func(t *testing.T) {
err := utils.Unzip(filepath.Join("unzip_test", "compressed_test.zip"))
require.True(t, err == nil, err)
require.NoError(t, err)

f, err := os.Open(filepath.Join("unzip_test", "compressed_test"))
require.NoError(t, err)

t.Cleanup(func() {
f.Close()
Expand Down Expand Up @@ -203,16 +204,16 @@ func TestUnzip(t *testing.T) {
os.Remove(filepath.Join("unzip_test", "compressed_test"))
})

require.True(t, err == nil, err)
require.NoError(t, err)
b, err := io.ReadAll(f)
require.True(t, err == nil, err)
require.NoError(t, err)

builder := strings.Builder{}
for i := 0; i < 50; i++ {
builder.WriteString("compressed test file with zip\n")
builder.WriteString(strconv.Itoa(i+1) + " compressed test file with zip\n")
}

require.Equal(t, string(b), builder.String())
require.Equal(t, builder.String(), string(b), "lines from actual %d", strings.Count(string(b), "\n"))
})
}

Expand Down

0 comments on commit 36f8a22

Please sign in to comment.