aboutsummaryrefslogtreecommitdiff
path: root/src/clients/paludis/sync.cc
blob: 4ae0390deca809abb3c43a20dafc714c58467d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <src/output/colour.hh>
#include "sync.hh"
#include <paludis/sync_task.hh>
#include <paludis/syncer.hh>
#include <iomanip>
#include <iostream>
#include <string>

/** \file
 * Handle the --sync action for the main paludis program.
 */

using namespace paludis;
using std::cerr;
using std::cout;
using std::endl;

namespace
{
    class OurSyncTask :
        public SyncTask
    {
        private:
            int _return_code;

        public:
            OurSyncTask(tr1::shared_ptr<Environment> env) :
                SyncTask(env.get()),
                _return_code(0)
            {
            }

            virtual void on_sync_all_pre();
            virtual void on_sync_pre(const RepositoryName &);
            virtual void on_sync_post(const RepositoryName &);
            virtual void on_sync_skip(const RepositoryName &);
            virtual void on_sync_fail(const RepositoryName &, const SyncFailedError &);
            virtual void on_sync_succeed(const RepositoryName &);
            virtual void on_sync_all_post();

            int return_code() const
            {
                return _return_code;
            }
    };

    void
    OurSyncTask::on_sync_all_pre()
    {
    }

    void
    OurSyncTask::on_sync_pre(const RepositoryName & r)
    {
        cout << colour(cl_heading, "Sync " + stringify(r)) << endl;
        cerr << xterm_title("Syncing " + stringify(r));
    }

    void
    OurSyncTask::on_sync_post(const RepositoryName &)
    {
    }

    void
    OurSyncTask::on_sync_skip(const RepositoryName & r)
    {
        cout << "Sync " << r << " skipped" << endl;
    }

    void
    OurSyncTask::on_sync_succeed(const RepositoryName & r)
    {
        cout << "Sync " << r << " completed" << endl;
    }

    void
    OurSyncTask::on_sync_fail(const RepositoryName & r, const SyncFailedError & e)
    {
        _return_code |= 1;
        cout << endl;
        cerr << "Sync error:" << endl;
        cerr << "  * " << e.backtrace("\n  * ") << e.message() << endl;
        cerr << endl;
        cout << "Sync " << r << " failed" << endl;
    }

    void
    OurSyncTask::on_sync_all_post()
    {
        cout << endl;
    }
}

int do_sync(tr1::shared_ptr<Environment> env)
{
    Context context("When performing sync action from command line:");

    OurSyncTask task(env);

    for (CommandLine::ParametersConstIterator q(CommandLine::get_instance()->begin_parameters()),
            q_end(CommandLine::get_instance()->end_parameters()) ; q != q_end ; ++q)
        task.add_target(*q);

    task.execute();
    cout << endl;

    return task.return_code();
}