Skip to content

Commit

Permalink
fixed bug where vars weren't editable in the UI #668 #655
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Mar 28, 2021
1 parent 5e1f50e commit 54ab069
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
21 changes: 18 additions & 3 deletions app/frontend/src/elements/Variables.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script>
import { onMount, createEventDispatcher } from 'svelte'
import { createEventDispatcher } from 'svelte'
import { refreshAllPlugins } from '../rpc.svelte'
import Button from './Button.svelte'
// dispatch handles the following events:
// on:change - fired when the values change
Expand All @@ -13,6 +12,21 @@
export let values
export let disabled = false
let onChangeDebounceTimeout
function onChange() {
clearTimeout(onChangeDebounceTimeout)
onChangeDebounceTimeout = setTimeout(fireOnChangeEvent, 300)
}
function fireOnChangeEvent() {
dispatch('change')
}
function refresh() {
dispatch('change')
setTimeout(refreshAllPlugins, 100)
}
</script>

{#if variables && variables.length && values}
Expand All @@ -29,6 +43,7 @@
</td>
<td class='py-3 pr-6'>
<VariableInput
on:change={onChange}
values={values}
variable={variable}
disabled={disabled}
Expand All @@ -41,7 +56,7 @@
{/each}
</table>
<p class='pt-3 pb-6 opacity-75 text-sm'>
<span class='bg-yellow-100 bg-opacity-50 dark:bg-transparent'>💡 You must refresh the plugin for changes to take effect.</span>
<span class='bg-yellow-100 bg-opacity-50 dark:bg-transparent'>💡 You must <a href='#/refresh-all' class='underline' on:click|preventDefault={refresh}>refresh the plugin</a> for changes to take effect.</span>
</p>
</div>
{/if}
4 changes: 2 additions & 2 deletions pkg/plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (p *Plugin) loadVariablesFromJSONFile() ([]string, error) {
f, err := os.Open(variablesJSONFilename)
if err != nil && os.IsNotExist(err) {
// no .vars.json file - no probs
p.Debugf("%s, no such file:", variablesJSONFilename)
p.Debugf("(skipping) no variable file: %s", variablesJSONFilename)
return nil, nil
} else if err != nil {
p.Debugf("ERR: %s", variablesJSONFilename, err)
Expand All @@ -332,7 +332,7 @@ func (p *Plugin) loadVariablesFromJSONFile() ([]string, error) {
if err != nil {
return nil, err
}
p.Debugf("vars json: %s", string(b))
p.Debugf("%s: %s", variablesJSONFilename, string(b))
var varmap map[string]interface{}
if err := json.Unmarshal(b, &varmap); err != nil {
return nil, errors.Wrap(err, "json.Unmarshal")
Expand Down
34 changes: 0 additions & 34 deletions pkg/plugins/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package plugins

import (
"context"
"log"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -265,38 +263,6 @@ It also contains a very long line which should be wrapped, because otherwise it
is.Equal(len(p.Items.ExpandedItems), 3)

}

func TestEnvironmentVariables(t *testing.T) {
is := is.New(t)

err := os.Setenv("XBAR_TEST_EXPLICIT_VAR", "explicit")
is.NoErr(err)
t.Cleanup(func() {
err := os.Unsetenv("XBAR_TEST_EXPLICIT_VAR")
is.NoErr(err)
})

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

p := &Plugin{
Command: filepath.Join("testdata", "vars-test", "plugin.sh"),
Debugf: DebugfNoop,
Timeout: 1 * time.Second,
RefreshInterval: RefreshInterval{N: 250, Unit: "milliseconds"},
CycleInterval: 500 * time.Millisecond,
}
p.Run(ctx)

log.Printf("%+v\n", p.Items.ExpandedItems)

is.Equal(len(p.Items.CycleItems), 2)
is.Equal(p.Items.CycleItems[0].Text, `XBAR_TEST_EXPLICIT_VAR=explicit`)
is.Equal(p.Items.CycleItems[1].Text, `XBAR_TEST_SET_IN_VARS_JSON=json`)

}

func TestCleanFilename(t *testing.T) {
is := is.New(t)

Expand Down
32 changes: 32 additions & 0 deletions pkg/plugins/variables_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
package plugins

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

"github.com/matryer/is"
)

func TestEnvironmentVariables(t *testing.T) {
is := is.New(t)

err := os.Setenv("XBAR_TEST_EXPLICIT_VAR", "explicit")
is.NoErr(err)
t.Cleanup(func() {
err := os.Unsetenv("XBAR_TEST_EXPLICIT_VAR")
is.NoErr(err)
})

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

p := &Plugin{
Command: filepath.Join("testdata", "vars-test", "plugin.sh"),
Debugf: DebugfNoop,
Timeout: 1 * time.Second,
RefreshInterval: RefreshInterval{N: 250, Unit: "milliseconds"},
CycleInterval: 500 * time.Millisecond,
}
p.Run(ctx)

is.Equal(len(p.Items.CycleItems), 2)
is.Equal(p.Items.CycleItems[0].Text, `XBAR_TEST_EXPLICIT_VAR=explicit`) // inherited
is.Equal(p.Items.CycleItems[1].Text, `XBAR_TEST_SET_IN_VARS_JSON=json`) // in vars.json file

}

func TestVariablesPersistence(t *testing.T) {
is := is.New(t)

Expand Down

0 comments on commit 54ab069

Please sign in to comment.