Browse Source

fix: commands with required arguments

failing w/ confusing stacktraces. Also exit correctly when a nonexistent
graph is given
Gabriel Horner 4 months ago
parent
commit
35c2952f6b

+ 8 - 6
deps/cli/src/logseq/cli.cljs

@@ -66,22 +66,22 @@
     :fn (lazy-load-fn 'logseq.cli.commands.graph/list-graphs)}
    {:cmds ["show"] :desc "Show DB graph(s) info"
     :fn (lazy-load-fn 'logseq.cli.commands.graph/show-graph)
-    :args->opts [:graphs] :coerce {:graphs []}}
+    :args->opts [:graphs] :coerce {:graphs []} :require [:graphs]}
    {:cmds ["search"]
     :fn (lazy-load-fn 'logseq.cli.commands.search/search)
     :desc "Search current DB graph"
-    :args->opts [:search-terms] :coerce {:search-terms []}
+    :args->opts [:search-terms] :coerce {:search-terms []} :require [:search-terms]
     :spec cli-spec/search}
    {:cmds ["query"] :desc "Query DB graph(s)"
     :fn (lazy-load-fn 'logseq.cli.commands.query/query)
-    :args->opts [:graph :args] :coerce {:args []} :no-keyword-opts true
+    :args->opts [:graph :args] :coerce {:args []} :no-keyword-opts true :require [:graph :args]
     :spec cli-spec/query}
    {:cmds ["export-edn"] :desc "Export DB graph as EDN"
     :fn (lazy-load-fn 'logseq.cli.commands.export-edn/export)
-    :args->opts [:graph]
+    :args->opts [:graph] :require [:graph]
     :spec cli-spec/export-edn}
    {:cmds ["help"] :fn command-help :desc "Print a command's help"
-    :args->opts [:command]}
+    :args->opts [:command] :require [:command]}
    {:cmds []
     :spec default-spec
     :fn default-command}])
@@ -101,7 +101,9 @@
                   {:error-fn (fn [{:keys [cause msg option] type' :type :as data}]
                                (if (and (= :org.babashka/cli type')
                                         (= :require cause))
-                                 (println "Error: Command missing required option" option)
+                                 (println "Error: Command missing required"
+                                          (if (get-in data [:spec option]) "option" "argument")
+                                          option)
                                  (throw (ex-info msg data)))
                                (js/process.exit 1))})
     (catch ^:sci/error js/Error e

+ 1 - 1
deps/cli/src/logseq/cli/commands/export_edn.cljs

@@ -21,4 +21,4 @@
          (fs/writeFileSync (:file options)
                            (with-out-str (pprint/pprint export-map))))
        (pprint/pprint export-map)))
-    (println "Graph" (pr-str graph) "does not exist")))
+    (cli-util/error "Graph" (pr-str graph) "does not exist")))

+ 1 - 1
deps/cli/src/logseq/cli/commands/graph.cljs

@@ -34,7 +34,7 @@
                          (str "https://github.com/logseq/logseq/commit/" (kv-value :logseq.kv/graph-git-sha))])
                   (d/entity @conn :logseq.kv/import-type)
                   (conj ["Graph imported by" (kv-value :logseq.kv/import-type)])))))
-        (println "Graph" (pr-str graph) "does not exist")))))
+        (cli-util/error "Graph" (pr-str graph) "does not exist")))))
 
 (defn list-graphs
   []

+ 1 - 1
deps/cli/src/logseq/cli/commands/query.cljs

@@ -82,7 +82,7 @@
           (when (> (count graphs') 1)
             (println "Results for graph" (pr-str graph')))
           (pprint/pprint results))
-        (println "Graph" (pr-str graph') "does not exist")))))
+        (cli-util/error "Graph" (pr-str graph') "does not exist")))))
 
 (defn query
   [{{:keys [graph args api-query-token]} :opts :as m}]

+ 6 - 0
deps/cli/src/logseq/cli/util.cljs

@@ -42,4 +42,10 @@
         (js/console.log "Make sure the HTTP API Server is turned on."))
     :else
     (js/console.error "Error:" err))
+  (js/process.exit 1))
+
+(defn error
+  "Prints error and then exits"
+  [& strings]
+  (apply println "Error:" strings)
   (js/process.exit 1))