Ucode in interactive shell

Is it possible to run ucode in an interactive shell to play and learn?

Not like python, no. There’s a proposal that’s not in yet for debug, tho.

But if you just want to play around, get used to running ucode [-l<lib>] -e ‘<ucode stuff>’. Building ucode locally on your system makes it easily accessible.

1 Like

Thanks that is what I am doing now.
I have Bash and C skills and ucode is something new I want to learn :slight_smile:

A good resource for specific examples is the tests, like this one for the stdlib exists function: https://github.com/jow-/ucode/blob/master/tests/custom/03_stdlib/03_exists

They are all written in template mode, though, so are sort of confusing at first glance. Just grab out the parts inside the {% ... %} or {{ ... }} and they become "normal code".

1 Like

Thanks that is very useful

Any other good hints on how to jump in?

1 Like

Read the docs and start playing with code...

$ cat json_pp
#!/usr/bin/ucode -S

import { stdin } from "fs";

function sorted(o)
{
        switch (type(o)) {
                case "array":
                        o = sort(map(o, sorted));
                        break;
                case "object":
                        for (let k, v in o) {
                                o[k] = sorted(v);
                        }
                        o = sort(o);
                        break;
        }
        return o;
}

let blob = json(stdin.read("all"));
if (ARGV[0] == "--sort") blob = sorted(blob);

printf("%.4J\n", blob);

$ ubus call system board | ./json_pp --sort
...
2 Likes

I've got a million of them. ubus calls are easy, you get back json, which ends up in an object.

#!/usr/bin/env ucode

import * as ubus from "ubus";

u = ubus.connect();
b = u.call("system", "board");
u.disconnect();

printf("%s %s\n", b.release.target, b.board_name);

uci is likewise easy as there's a package (ucode-mod-uci) that lets you talk to the library directly. This package should already be installed just about everywhere, as it's a dependency of firewall4.

#!/usr/bin/ucode -S

import { cursor } from 'uci';
let uci = cursor();

printf("configs:\n");
let configs = uci.configs();
for (let item in configs) {
    printf("  %s\n", item);
}

function show_section(config, section)
{
    printf("%s.%s:\n", config, section);
    for (let item, value in uci.get_all(config, section)) {
        printf("  %s = %s\n", item, value);
    }
}

show_section("dhcp", "@host[0]");
show_section("system", "ntp");
4 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.