1- //! OpenCode CLI detection module
2-
31use serde:: Serialize ;
42use std:: path:: PathBuf ;
53use std:: process:: Command ;
64use which:: which;
75
8- /// Status of OpenCode CLI installation
96#[ derive( Debug , Clone , Serialize , PartialEq ) ]
107pub struct OpenCodeStatus {
118 pub found : bool ,
@@ -23,54 +20,27 @@ impl Default for OpenCodeStatus {
2320 }
2421}
2522
26- #[ tauri:: command]
27- pub fn check_opencode ( ) -> OpenCodeStatus {
28- // First check bundled sidecar next to our own binary
29- if let Ok ( exe) = std:: env:: current_exe ( ) {
30- if let Some ( bin_dir) = exe. parent ( ) {
31- let target_triple = env ! ( "TARGET" ) ;
32- let bundled = bin_dir. join ( format ! ( "opencode-{}" , target_triple) ) ;
33- if bundled. exists ( ) {
34- let version = Command :: new ( & bundled)
35- . arg ( "--version" )
36- . output ( )
37- . ok ( )
38- . and_then ( |output| {
39- if output. status . success ( ) {
40- String :: from_utf8 ( output. stdout ) . ok ( )
41- } else {
42- String :: from_utf8 ( output. stderr ) . ok ( )
43- }
44- } )
45- . map ( |s| s. trim ( ) . to_string ( ) )
46- . filter ( |s| !s. is_empty ( ) ) ;
47-
48- return OpenCodeStatus {
49- found : true ,
50- version,
51- path : Some ( bundled. to_string_lossy ( ) . to_string ( ) ) ,
52- } ;
23+ fn get_opencode_version ( binary_path : & std:: path:: Path ) -> Option < String > {
24+ Command :: new ( binary_path)
25+ . arg ( "--version" )
26+ . output ( )
27+ . ok ( )
28+ . and_then ( |output| {
29+ if output. status . success ( ) {
30+ String :: from_utf8 ( output. stdout ) . ok ( )
31+ } else {
32+ String :: from_utf8 ( output. stderr ) . ok ( )
5333 }
54- }
55- }
34+ } )
35+ . map ( |s| s. trim ( ) . to_string ( ) )
36+ . filter ( |s| !s. is_empty ( ) )
37+ }
5638
57- // Fall back to system PATH
39+ #[ tauri:: command]
40+ pub fn check_opencode ( ) -> OpenCodeStatus {
5841 match which ( "opencode" ) {
5942 Ok ( path) => {
60- let version = Command :: new ( & path)
61- . arg ( "--version" )
62- . output ( )
63- . ok ( )
64- . and_then ( |output| {
65- if output. status . success ( ) {
66- String :: from_utf8 ( output. stdout ) . ok ( )
67- } else {
68- String :: from_utf8 ( output. stderr ) . ok ( )
69- }
70- } )
71- . map ( |s| s. trim ( ) . to_string ( ) )
72- . filter ( |s| !s. is_empty ( ) ) ;
73-
43+ let version = get_opencode_version ( & path) ;
7444 OpenCodeStatus {
7545 found : true ,
7646 version,
@@ -93,11 +63,6 @@ pub fn pick_local_folder() -> Option<String> {
9363 . map ( |path| path. to_string_lossy ( ) . to_string ( ) )
9464}
9565
96- #[ allow( dead_code) ]
97- fn check_binary_exists ( binary_name : & str ) -> bool {
98- which ( binary_name) . is_ok ( )
99- }
100-
10166fn default_home_dir ( ) -> Option < PathBuf > {
10267 std:: env:: var_os ( "HOME" )
10368 . or_else ( || std:: env:: var_os ( "USERPROFILE" ) )
@@ -116,18 +81,6 @@ mod tests {
11681 assert ! ( status. path. is_none( ) ) ;
11782 }
11883
119- #[ test]
120- fn test_check_binary_exists_with_common_binary ( ) {
121- assert ! ( check_binary_exists( "ls" ) ) ;
122- }
123-
124- #[ test]
125- fn test_check_binary_exists_with_nonexistent_binary ( ) {
126- assert ! ( !check_binary_exists(
127- "this-binary-definitely-does-not-exist-12345"
128- ) ) ;
129- }
130-
13184 #[ test]
13285 fn test_check_opencode_returns_valid_structure ( ) {
13386 let status = check_opencode ( ) ;
0 commit comments