1 | |
---|
2 | #include "Sources.hpp" |
---|
3 | #include "Commands.hpp" |
---|
4 | #include "Cache.hpp" |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <fstream> |
---|
8 | |
---|
9 | #include <glibmm.h> |
---|
10 | #include <giomm.h> |
---|
11 | |
---|
12 | using namespace net::lliurex::apt; |
---|
13 | using namespace std; |
---|
14 | using namespace std::placeholders; |
---|
15 | |
---|
16 | class Application |
---|
17 | { |
---|
18 | |
---|
19 | |
---|
20 | public: |
---|
21 | |
---|
22 | /*! mandatory sources */ |
---|
23 | vector<Sources> conf; |
---|
24 | |
---|
25 | /*! optional sources */ |
---|
26 | vector<Sources> optional; |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | Application(int argc,char * argv[]) |
---|
31 | { |
---|
32 | |
---|
33 | //this should be call before calling any other command |
---|
34 | command::Init(); |
---|
35 | |
---|
36 | if(argc<2) |
---|
37 | PrintUsage(); |
---|
38 | else |
---|
39 | { |
---|
40 | string arg(argv[1]); |
---|
41 | bool found = false; |
---|
42 | |
---|
43 | if(arg=="list") |
---|
44 | { |
---|
45 | conf = command::LoadConf(); |
---|
46 | command::List(conf); |
---|
47 | found=true; |
---|
48 | } |
---|
49 | |
---|
50 | if(arg=="show") |
---|
51 | { |
---|
52 | command::Show(); |
---|
53 | found=true; |
---|
54 | } |
---|
55 | |
---|
56 | if(arg=="check") |
---|
57 | { |
---|
58 | conf = command::LoadConf(); |
---|
59 | Sources src(Sources::SourcesList); |
---|
60 | |
---|
61 | vector<command::Match> status = command::Check(src,conf); |
---|
62 | |
---|
63 | for(command::Match m : status) |
---|
64 | { |
---|
65 | string txt=""; |
---|
66 | |
---|
67 | switch(m.status) |
---|
68 | { |
---|
69 | case FindLineStatus::Found: |
---|
70 | txt="complete"; |
---|
71 | break; |
---|
72 | |
---|
73 | case FindLineStatus::Partial: |
---|
74 | txt="partial"; |
---|
75 | break; |
---|
76 | } |
---|
77 | |
---|
78 | if(txt!="") |
---|
79 | { |
---|
80 | cout<<txt<<" ["<<m.sources->name["C"]<<"]"<<endl; |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | found=true; |
---|
86 | } |
---|
87 | |
---|
88 | if(arg=="set") |
---|
89 | { |
---|
90 | conf = command::LoadConf(); |
---|
91 | |
---|
92 | vector<string> targets; |
---|
93 | |
---|
94 | for(int n=2;n<argc;n++) |
---|
95 | { |
---|
96 | targets.push_back(argv[n]); |
---|
97 | } |
---|
98 | |
---|
99 | Sources src(Sources::SourcesList); |
---|
100 | src=command::Set(src,conf,targets); |
---|
101 | src.Print(); |
---|
102 | Write(src,Sources::SourcesList); |
---|
103 | |
---|
104 | found=true; |
---|
105 | } |
---|
106 | |
---|
107 | if(arg=="reset") |
---|
108 | { |
---|
109 | conf = command::LoadConf(); |
---|
110 | |
---|
111 | vector<string> targets; |
---|
112 | |
---|
113 | for(int n=2;n<argc;n++) |
---|
114 | { |
---|
115 | targets.push_back(argv[n]); |
---|
116 | } |
---|
117 | |
---|
118 | Sources src(Sources::SourcesList); |
---|
119 | src=command::Set(src,conf,targets,true); |
---|
120 | src.Print(); |
---|
121 | Write(src,Sources::SourcesList); |
---|
122 | found=true; |
---|
123 | } |
---|
124 | |
---|
125 | if(arg=="add") |
---|
126 | { |
---|
127 | if(argc<=3) |
---|
128 | { |
---|
129 | Sources src(Sources::SourcesList); |
---|
130 | src=command::Add(src,argv[2]); |
---|
131 | src.Print(); |
---|
132 | src.Write(Sources::SourcesList); |
---|
133 | found = true; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | if(!found) |
---|
140 | { |
---|
141 | PrintUsage(); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /*! |
---|
147 | There is nothing to free |
---|
148 | */ |
---|
149 | ~Application() |
---|
150 | { |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | /*! |
---|
156 | Just prints out usage to stdout |
---|
157 | */ |
---|
158 | void PrintUsage() |
---|
159 | { |
---|
160 | cout<<"Usage:"<<endl; |
---|
161 | cout<<"llx-apt-cmd [COMMAND]"<<endl; |
---|
162 | |
---|
163 | cout<<"Available commands:"<<endl; |
---|
164 | |
---|
165 | cout<<"show\tSummarize sources.list"<<endl; |
---|
166 | cout<<"list\tShows installed profiles"<<endl; |
---|
167 | cout<<"check\tCompares sources.list with installed profiles"<<endl; |
---|
168 | cout<<"set [PROFILE1] [PROFILE2] [...]\tSets given profiles to sources.list"<<endl; |
---|
169 | cout<<"reset [PROFILE1] [PROFILE2] [...]\tSame as set command, but it wipes out current sources.list content, instead of merging it"<<endl; |
---|
170 | cout<<"add [LINE]\tAppends a new line to sources.list"<<endl; |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | void Write(Sources & src,const string & filename) |
---|
175 | { |
---|
176 | fstream fs (filename,std::fstream::out); |
---|
177 | |
---|
178 | if(!fs.good()) |
---|
179 | throw runtime_error("Failed to write sources.list file"); |
---|
180 | |
---|
181 | for(int n=0;n<src.lines.size();n++) |
---|
182 | { |
---|
183 | fs<<src.lines[n].ToString()<<endl; |
---|
184 | } |
---|
185 | |
---|
186 | fs.close(); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | }; |
---|
191 | |
---|
192 | int main(int argc,char * argv[]) |
---|
193 | { |
---|
194 | Application app(argc,argv); |
---|
195 | |
---|
196 | return 0; |
---|
197 | } |
---|