Coverage for src / mysingle / cli / protos / commands / init.py: 0%

80 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-02 00:58 +0900

1""" 

2Init 명령 - mysingle 패키지 초기화. 

3 

4통합 구조에서는 더 이상 submodule이 아닌 단일 패키지로 관리됩니다. 

5""" 

6 

7from __future__ import annotations 

8 

9import argparse 

10import subprocess 

11 

12from ..models import ProtoConfig 

13from ..utils import Color, LogLevel, colorize, log, log_header 

14 

15 

16def execute(args: argparse.Namespace, config: ProtoConfig) -> int: 

17 """초기화 명령 실행""" 

18 log_header("MySingle 패키지 초기화") 

19 

20 # Git 저장소 확인 

21 try: 

22 result = subprocess.run( 

23 ["git", "rev-parse", "--git-dir"], 

24 cwd=config.repo_root, 

25 capture_output=True, 

26 text=True, 

27 check=False, 

28 ) 

29 if result.returncode == 0: 

30 log( 

31 f"Git 저장소 확인: {colorize(str(config.repo_root), Color.BRIGHT_GREEN)}", 

32 LogLevel.SUCCESS, 

33 ) 

34 else: 

35 log("Git 저장소가 아닙니다.", LogLevel.ERROR) 

36 return 1 

37 except FileNotFoundError: 

38 log("Git이 설치되어 있지 않습니다.", LogLevel.ERROR) 

39 return 1 

40 

41 # 브랜치 확인 

42 result = subprocess.run( 

43 ["git", "branch", "--show-current"], 

44 cwd=config.repo_root, 

45 capture_output=True, 

46 text=True, 

47 check=False, 

48 ) 

49 current_branch = result.stdout.strip() 

50 log(f"현재 브랜치: {colorize(current_branch, Color.BRIGHT_GREEN)}", LogLevel.INFO) 

51 

52 # 원격 저장소 확인 

53 result = subprocess.run( 

54 ["git", "remote", "-v"], 

55 cwd=config.repo_root, 

56 capture_output=True, 

57 text=True, 

58 check=False, 

59 ) 

60 if result.returncode == 0 and result.stdout: 

61 log("원격 저장소:", LogLevel.INFO) 

62 for line in result.stdout.strip().split("\n"): 

63 if "github.com/Br0therDan/mysingle-pack" in line: 

64 print(f" {colorize(line, Color.GREEN)}") 

65 else: 

66 print(f" {line}") 

67 else: 

68 log("원격 저장소가 설정되어 있지 않습니다.", LogLevel.WARNING) 

69 

70 # Buf 설치 확인 

71 try: 

72 result = subprocess.run( 

73 ["buf", "--version"], 

74 capture_output=True, 

75 text=True, 

76 check=False, 

77 ) 

78 if result.returncode == 0: 

79 version = result.stdout.strip() 

80 log(f"Buf 설치 확인: {colorize(version, Color.GREEN)}", LogLevel.SUCCESS) 

81 else: 

82 log("Buf가 설치되어 있지 않습니다.", LogLevel.WARNING) 

83 log("설치 방법: https://buf.build/docs/installation", LogLevel.INFO) 

84 return 1 

85 except FileNotFoundError: 

86 log("Buf가 설치되어 있지 않습니다.", LogLevel.ERROR) 

87 log("설치 방법: https://buf.build/docs/installation", LogLevel.INFO) 

88 return 1 

89 

90 # 필수 디렉터리 확인 

91 directories = [ 

92 ("Proto 원본", config.proto_root), 

93 ("Proto 생성", config.generated_root), 

94 ] 

95 

96 log("\n필수 디렉터리 확인:", LogLevel.INFO) 

97 all_exist = True 

98 for name, path in directories: 

99 if path.exists(): 

100 log(f"{name}: {path}", LogLevel.SUCCESS) 

101 else: 

102 log(f"{name}: {path} (없음)", LogLevel.ERROR) 

103 all_exist = False 

104 

105 if not all_exist: 

106 log("\n필수 디렉터리가 존재하지 않습니다.", LogLevel.ERROR) 

107 return 1 

108 

109 # buf.yaml 확인 

110 buf_yaml = config.proto_root / "buf.yaml" 

111 buf_gen_yaml = config.proto_root / "buf.gen.yaml" 

112 

113 log("\nBuf 설정 파일 확인:", LogLevel.INFO) 

114 if buf_yaml.exists(): 

115 log(f" ✅ buf.yaml: {buf_yaml}", LogLevel.SUCCESS) 

116 else: 

117 log(f" ❌ buf.yaml: {buf_yaml} (없음)", LogLevel.ERROR) 

118 all_exist = False 

119 

120 if buf_gen_yaml.exists(): 

121 log(f" ✅ buf.gen.yaml: {buf_gen_yaml}", LogLevel.SUCCESS) 

122 else: 

123 log(f" ❌ buf.gen.yaml: {buf_gen_yaml} (없음)", LogLevel.ERROR) 

124 all_exist = False 

125 

126 if not all_exist: 

127 log("\nBuf 설정 파일이 존재하지 않습니다.", LogLevel.ERROR) 

128 return 1 

129 

130 # 패키지 정보 출력 

131 log("\n" + "=" * 60, LogLevel.INFO) 

132 log("📦 MySingle 통합 패키지 정보", LogLevel.INFO) 

133 log(f" - 저장소: {config.repo_root}", LogLevel.INFO) 

134 log(f" - Proto 원본: {config.proto_root}", LogLevel.INFO) 

135 log(f" - Proto 생성: {config.generated_root}", LogLevel.INFO) 

136 log("=" * 60, LogLevel.INFO) 

137 

138 log("\n✅ 초기화 완료!", LogLevel.SUCCESS) 

139 log( 

140 f"\n다음 명령으로 상태를 확인하세요: {colorize('mysingle-proto status', Color.BRIGHT_YELLOW)}", 

141 LogLevel.INFO, 

142 ) 

143 log( 

144 f"Proto 생성: {colorize('mysingle-proto generate', Color.BRIGHT_YELLOW)}", 

145 LogLevel.INFO, 

146 ) 

147 

148 return 0 

149 

150 

151def execute_interactive(config: ProtoConfig) -> int: 

152 """대화형 모드로 init 명령 실행""" 

153 log_header("MySingle 패키지 초기화") 

154 

155 # 기본 실행 (--check-only 없이) 

156 args = argparse.Namespace(check_only=False) 

157 return execute(args, config) 

158 

159 

160def setup_parser(parser: argparse.ArgumentParser) -> None: 

161 """Init 명령 파서 설정""" 

162 parser.add_argument( 

163 "--check-only", 

164 action="store_true", 

165 help="초기화 없이 현재 상태만 확인", 

166 )