1- use crate :: { LegacyProject , into_json_value, we_need_proper_json_output_here} ;
1+ use crate :: LegacyProject ;
2+ use crate :: utils:: { Output , OutputFormat , into_json_value, we_need_proper_json_output_here} ;
23use anyhow:: bail;
3- use atty:: Stream ;
44use but_core:: ref_metadata:: StackId ;
55use but_settings:: AppSettings ;
66use but_workspace:: legacy:: ui:: StackEntry ;
@@ -70,9 +70,8 @@ pub enum Subcommands {
7070pub async fn handle (
7171 cmd : Option < Subcommands > ,
7272 ctx : & but_ctx:: Context ,
73- json : bool ,
73+ out : & mut Output ,
7474) -> anyhow:: Result < serde_json:: Value > {
75- let mut stdout = io:: stdout ( ) ;
7675 let legacy_project = & ctx. legacy_project ;
7776 match cmd {
7877 None => {
@@ -147,18 +146,19 @@ pub async fn handle(
147146 } ,
148147 ) ?;
149148
150- if json {
151- let response = json:: BranchNewOutput {
152- branch : branch_name,
153- anchor : anchor_for_json,
154- } ;
155- writeln ! ( stdout, "{}" , serde_json:: to_string_pretty( & response) ?) ?;
156- } else if atty:: is ( Stream :: Stdout ) {
157- writeln ! ( stdout, "Created branch {branch_name}" ) . ok ( ) ;
158- } else {
159- writeln ! ( stdout, "{branch_name}" ) . ok ( ) ;
149+ let json = json:: BranchNewOutput {
150+ branch : branch_name. clone ( ) ,
151+ anchor : anchor_for_json,
152+ } ;
153+ match out. format {
154+ OutputFormat :: Human => {
155+ writeln ! ( out, "Created branch {branch_name}" ) . ok ( ) ;
156+ }
157+ OutputFormat :: Shell => {
158+ writeln ! ( out, "{branch_name}" ) . ok ( ) ;
159+ }
160160 }
161- Ok ( we_need_proper_json_output_here ( ) )
161+ Ok ( into_json_value ( json ) )
162162 }
163163 Some ( Subcommands :: Delete { branch_name, force } ) => {
164164 let stacks = but_api:: workspace:: stacks (
@@ -178,11 +178,11 @@ pub async fn handle(
178178 }
179179 }
180180
181- writeln ! ( stdout , "Branch '{}' not found in any stack" , branch_name) . ok ( ) ;
181+ writeln ! ( out , "Branch '{}' not found in any stack" , branch_name) . ok ( ) ;
182182 Ok ( we_need_proper_json_output_here ( ) )
183183 }
184184 Some ( Subcommands :: Apply { branch_name } ) => {
185- apply:: apply ( ctx, & branch_name, json ) . map ( into_json_value)
185+ apply:: apply ( ctx, & branch_name, out ) . map ( into_json_value)
186186 }
187187 Some ( Subcommands :: Unapply { branch_name, force } ) => {
188188 let stacks = but_api:: workspace:: stacks (
@@ -202,7 +202,7 @@ pub async fn handle(
202202 }
203203 }
204204
205- writeln ! ( stdout , "Branch '{}' not found in any stack" , branch_name) . ok ( ) ;
205+ writeln ! ( out , "Branch '{}' not found in any stack" , branch_name) . ok ( ) ;
206206 Ok ( we_need_proper_json_output_here ( ) )
207207 }
208208 }
0 commit comments