示例#1
0
文件: Program.cs 项目: syat/ossu
        static bool Confirm(Context ctx, String prompt)
        {
            while (true)
            {
                Console.Write(prompt);
                String input = Console.ReadLine().ToLower();
                if (input == "y" || input == "yes")
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
示例#2
0
文件: Program.cs 项目: syat/ossu
        static int Main(string[] args)
        {
            Context ctx = new Context();
            ctx.Option = new Option();

            String command = null;
            List<String> queries = new List<string>();

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    switch (args[i].Substring(1))
                    {
                        case "p":
                            if (i + 1 < args.Length)
                            {
                                i++;
                                try
                                {
                                    ctx.WebProxy = new WebProxy(args[i]);
                                }
                                catch (UriFormatException ex)
                                {
                                    log.Warn("-pは無視されます  理由:プロキシサーバのアドレス形式が不正です");
                                }
                            }
                            else
                            {
                                log.Warn("-pは無視されます  理由:プロキシサーバのアドレスが指定されていません");
                            }
                            break;
                    }
                }
                else
                {
                    if (command == null)
                    {
                        command = args[i];
                    }
                    else
                    {
                        queries.Add(args[i]);
                    }
                }
            }

            if (command == null)
            {
                ShowUsage();
                return ExitCode.NG;
            }

            using (OssuCore ossu = new OssuCore(ctx, 5))
            {
                ossu.Log += ossu_Log;
                ossu.DownloadProgress += ossu_DownloadProgress;
                ossu.DownloadComplete += ossu_DownloadComplete;
                try
                {
                    switch (command)
                    {
                        case "list":
                            {
                                ListExclude exclude = ListExclude.None;

                                if (queries.Count == 0)
                                {
                                    queries.Add("*");
                                }
                                switch (queries[0])
                                {
                                    case "installed":
                                        exclude = ListExclude.NotInstalled;
                                        break;
                                    case "available":
                                        exclude = ListExclude.Installed;
                                        break;
                                }
                                RepositoryInfo repos = ossu.UpdateRepository();
                                PackageStateInfo packageStateInfo = ossu.ReadPackageState();
                                PackageInfo packageInfo = ossu.ListPackage(repos, queries, packageStateInfo, exclude);

                                if (packageInfo.Packages.Count == 0)
                                {
                                    log.Warn("該当するパッケージはありません");
                                }
                                else
                                {
                                    foreach (Package package in packageInfo.Packages.Values)
                                    {
                                        if (packageStateInfo.Packages.ContainsKey(package.Name))
                                        {
                                            PackageState packageState = packageStateInfo.Packages[package.Name];
                                            String state = "installed";
                                            Console.WriteLine("{0,-15} {1,-25}",
                                                package.Name, state);
                                        }
                                        else
                                        {
                                            Console.WriteLine("{0,-15}",
                                                package.Name);
                                        }
                                    }
                                }
                                break;
                            }
                        case "update":
                            {
                                if (queries.Count == 0)
                                {
                                    queries.Add("*");
                                }

                                RepositoryInfo repos = ossu.UpdateRepository();
                                PackageStateInfo packageStateInfo = ossu.ReadPackageState();
                                PackageInfo packageInfo = ossu.ListPackage(repos, queries, packageStateInfo, ListExclude.NotInstalled);
                                if (packageInfo.Packages.Count > 0)
                                {
                                    ossu.CheckUpdate(packageInfo, packageStateInfo);
                                    ossu.DownloadPackage(packageInfo, packageStateInfo);
                                }
                                break;
                            }
                        case "download":
                            {
                                //ossu.Download(query);
                                break;
                            }
                        case "install":
                            {
                                RepositoryInfo repos = ossu.UpdateRepository();
                                PackageStateInfo packageStateInfo = ossu.ReadPackageState();
                                PackageInfo packageInfo = ossu.ListPackage(repos, queries, packageStateInfo, ListExclude.Installed);
                                if (packageInfo.Packages.Count > 0)
                                {
                                    foreach (Package package in packageInfo.Packages.Values)
                                    {
                                        Console.WriteLine("{0}", package.Name);
                                        packageStateInfo.Packages.Add(package.Name, new PackageState() { Name = package.Name });
                                    }
                                    if (!Confirm(ctx, "インストールしますか [y/n]: "))
                                    {
                                        Console.WriteLine("キャンセルされました");
                                        break;
                                    }

                                    ossu.CheckUpdate(packageInfo, packageStateInfo);
                                    ossu.DownloadPackage(packageInfo, packageStateInfo);
                                }
                                else
                                {
                                    Console.WriteLine("インストール可能なパッケージはありません");
                                }
                                break;
                            }
                        default:
                            log.WarnFormat("不明なコマンド: {0}", command);
                            ShowUsage();
                            return ExitCode.NG;
                    }
                }
                catch (Exception ex)
                {
                    log.Warn("コマンド実行失敗", ex);
                }
            }
            return ExitCode.OK;
        }