Class: CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/fossgit/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], cliname = 'fossgit', filename = '.fossgit') ⇒ CLI

Returns a new instance of CLI.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fossgit/cli.rb', line 6

def initialize args=[], cliname='fossgit', filename='.fossgit'
  @args = args
  @config = Hash.new
  @name = cliname

  @config_filename = filename
  @home_config_file = File.join(Dir.home, @config_filename)

  configure @config_filename

  @switches = Hash.new
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def args
  @args
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def config
  @config
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def name
  @name
end

#switchesObject

Returns the value of attribute switches.



4
5
6
# File 'lib/fossgit/cli.rb', line 4

def switches
  @switches
end

Instance Method Details

#configure(file = @config_filename) ⇒ Object



19
20
21
22
# File 'lib/fossgit/cli.rb', line 19

def configure file=@config_filename
  load_home_config
  load_local_config file
end

#get_option(long_name, default = nil) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/fossgit/cli.rb', line 170

def get_option long_name, default=nil
  if val = (args.index "-#{long_name[0]}" or args.index "--#{long_name}")
    return (args.delete_at val and args.delete_at val)
  else
    return default
  end
end

#help_textObject



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fossgit/cli.rb', line 46

def help_text
  help = "\n    FossGit mirrors Fossil repositories to Git repositories.  You need:\n    \n    1. both Fossil SCM and Git installed\n    2. a Fossil repository with an open checkout to export\n    3. a Git repository to use as a mirror\n\n    USAGE:  \#{name} [-h | -v | [-c <CHECKOUT>] -t]\n            \#{name} [-c <CHECKOUT>] [-a | -r REMOTE | -l] [[-n] GITREPO]\n\n        By default, when exporting to local Git repository GITREPO, \#{name}\n        attempts to push updates to a configured upstream Git repository.  It\n        should harmlessly fail to push if there is no upstream repository\n        configured for the local Git repository.  If there is no GITREPO in\n        configuration (see CONFIG below), you must specify GITREPO when using\n        \#{name} to mirror your Fossil repository.\n\n    OPTIONS/ARGUMENTS:\n\n        -h | --help\n\n            Display this help text and exit, ignoring all other arguments.\n\n        -a | --all\n\n            Push to all configured remotes for the target Git repository.\n            Default is to push only to the configured \"origin\" remote.\n\n        -c <CHECKOUT> | --checkout <CHECKOUT>\n\n            Specify CHECKOUT, your Fossil repository's checkout location.\n            This is optional; you may simply use this tool from within an\n            open checkout of your Fossil repository instead.\n\n        -l | --local\n\n            Perform local-only mirror operations without pushing to a remote\n            Git repository.  By default, \#{name} pushes to an upstream Git\n            repository whenever it exports from Fossil to Git.\n\n        -n | --newgit\n        \n            Create a local Git repository mirror when it does not already\n            exist.  Use this option from the Fossil repository's directory\n            hierarchy root and specify the Git repository's location as for\n            updating a mirror via config file, GITREPO argument, or Fossil\n            project-name setting in `fossil info` output.\n\n        -r | --remotes <COMMA,SEPARATED,REMOTES>\n\n            Specify which of the Git mirror's configured remotes to push in a\n            comma separated list with no spaces.  This overrides `--all`.\n\n        -t | --text-export\n        \n            Dump export to STDOUT as text rather than sending it to Git.\n            This overrides the `-l` switch and GITREPO argument, if present.\n\n        -v | --version\n        \n            Show FossGit version number.\n\n        GITREPO\n        \n            Specify the location of the local Git mirror, if there is no\n            configuration file specification or to override configuration.\n\n    COMMAND EXAMPLES:\n\n        $ fossgit -h\n\n        $ fossgit -c ~/fossil_checkouts/projectname ~/git/projectname\n\n        $ cd ~/fossil_checkouts/projectname && fossgit ~/git/projectname\n\n    CONFIG:\n\n        A config file named \".fossgit\" can be used to set \#{name} defaults.\n\n        A universal config file can be placed in the home directory of the\n        user account that executes this command, in which case it will apply\n        every time that account executes \#{name}.  A config file can also be\n        placed in the root of an open Fossil repository, in which case it\n        will only apply when \#{name} is used for that particular repository.\n        If both files exist, settings from both will be used, but where both\n        have the same setting the more specific config (in the project\n        directory) overrides the equivalent setting from the universal\n        config.  This means a universal configuration can be used for the\n        most common case, but for specific projects that have different needs\n        you can place a config file the project repository.\n\n        These are the available configuration options:\n\n        * The \"gitdir\" setting can be used to specify a directory in which\n          Git repositories are kept.  In this case, \#{name} infers the Git\n          repository name from the \"project-name\" in \"fossil info\" output.\n\n        * The \"gitrepo\" setting can be used to specify the full path to a Git\n          repository, without letting \#{name} try to infer the repository\n          name.  This option overrides \"gitdir\" if both are set.  It is\n          probably a bad idea to set \"gitrepo\" in a universal config.  It is\n          intended to be set in a project-specific \".fossgit\" file because\n          chaos may ensue if all Fossil repositories use the same \"gitrepo\".\n\n        * The \"remotes\" setting can be used to specify which of your Git\n          mirror's configured remotes you want to use when pushing from Git.\n          As with the -r command line option, you can list one remote or list\n          several as a comma-separated list with no spaces.  The \"remotes\"\n          setting in a configuration file accepts the \"all\" value, equivalent\n          to the -a command line option.\n\n    CONFIG EXAMPLES:\n\n        gitdir: /home/username/fossrec/git\n\n        gitrepo: /home/username/fossrec\n\n        remotes: gitlab\n\n  EOF\nend\n"

#load_home_configObject



24
25
26
27
28
# File 'lib/fossgit/cli.rb', line 24

def load_home_config
  if File.exist? @home_config_file
    @config = YAML.load_file @home_config_file
  end
end

#load_local_config(file = @config_filename) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fossgit/cli.rb', line 30

def load_local_config file=@config_filename
  if File.exist? file
    YAML.load_file(file).tap do |local|
      local.each_key {|k| @config[k] = local[k] }
    end
  end
end

#option_switch?(long_name) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/fossgit/cli.rb', line 178

def option_switch? long_name
  args.delete "-#{long_name[0]}" or args.delete "--#{long_name}"
end

#parse_switches(opts) ⇒ Object



182
183
184
# File 'lib/fossgit/cli.rb', line 182

def parse_switches opts
  opts.each {|opt| switches[opt] = option_switch? opt }
end

#remotesObject



38
39
40
# File 'lib/fossgit/cli.rb', line 38

def remotes
  config['remotes']
end

#remotes=(gitremotes) ⇒ Object



42
43
44
# File 'lib/fossgit/cli.rb', line 42

def remotes= gitremotes
  config['remotes'] = gitremotes
end