From b8fcb30983098857602480c9264e31ec6cf9b58a Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Tue, 16 Aug 2022 00:11:41 +0800 Subject: [PATCH] improve error handling --- cmd/docker-credential-himitsu/erase.ha | 4 +--- cmd/docker-credential-himitsu/get.ha | 4 +--- cmd/docker-credential-himitsu/list.ha | 4 +--- cmd/docker-credential-himitsu/main.ha | 27 ++++++++++++++++++++------ cmd/docker-credential-himitsu/store.ha | 4 +--- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/docker-credential-himitsu/erase.ha b/cmd/docker-credential-himitsu/erase.ha index cd36079..9a11e77 100644 --- a/cmd/docker-credential-himitsu/erase.ha +++ b/cmd/docker-credential-himitsu/erase.ha @@ -5,9 +5,7 @@ use net; use os; use strings; -fn erase() void = { - const hi = client::connect()!; - defer net::close(hi)!; +fn erase(hi: net::socket) void = { const url = strings::fromutf8(bufio::scanline(os::stdin)! as []u8); const query = query::query { items = [ diff --git a/cmd/docker-credential-himitsu/get.ha b/cmd/docker-credential-himitsu/get.ha index 65e91fc..d654eee 100644 --- a/cmd/docker-credential-himitsu/get.ha +++ b/cmd/docker-credential-himitsu/get.ha @@ -7,9 +7,7 @@ use os; use shlex; use strings; -fn get() void = { - const hi = client::connect()!; - defer net::close(hi)!; +fn get(hi: net::socket) void = { const url = strings::fromutf8(bufio::scanline(os::stdin)! as []u8); const query = query::query { items = [ diff --git a/cmd/docker-credential-himitsu/list.ha b/cmd/docker-credential-himitsu/list.ha index 8d74930..97c2e93 100644 --- a/cmd/docker-credential-himitsu/list.ha +++ b/cmd/docker-credential-himitsu/list.ha @@ -6,9 +6,7 @@ use os; use shlex; use strings; -fn list() void = { - const hi = client::connect()!; - defer net::close(hi)!; +fn list(hi: net::socket) void = { const query = query::query { items = [ query::pair { key = "proto", value = "docker", private = false, optional = false}, diff --git a/cmd/docker-credential-himitsu/main.ha b/cmd/docker-credential-himitsu/main.ha index cbac3eb..8ecd39e 100644 --- a/cmd/docker-credential-himitsu/main.ha +++ b/cmd/docker-credential-himitsu/main.ha @@ -1,17 +1,32 @@ +use fmt; +use himitsu::client; +use net; use os; export fn main() void = { - assert(len(os::args) == 2); + if (len(os::args) != 2) { + fmt::fatalf("Usage: {} ", os::args[0]); + }; + + const sock = match (client::connect()) { + case let s: net::socket => + yield s; + case let err: client::error => + fmt::fatalf("Cannot connect to himitsud: {}", client::strerror(err)); + }; + defer net::close(sock)!; + switch (os::args[1]) { case "store" => - store(); + store(sock); case "get" => - get(); + get(sock); case "erase" => - erase(); + erase(sock); case "list" => - list(); + list(sock); case => - assert(false); + net::close(sock)!; + fmt::fatalf("Usage: {} ", os::args[0]); }; }; diff --git a/cmd/docker-credential-himitsu/store.ha b/cmd/docker-credential-himitsu/store.ha index ebdf9e0..5334b1e 100644 --- a/cmd/docker-credential-himitsu/store.ha +++ b/cmd/docker-credential-himitsu/store.ha @@ -4,9 +4,7 @@ use himitsu::query; use net; use os; -fn store() void = { - const hi = client::connect()!; - defer net::close(hi)!; +fn store(hi: net::socket) void = { const json = json::load(os::stdin)! as json::object; defer json::finish(json); const url = *(json::get(&json, "ServerURL") as *json::value) as str; -- 2.45.2