Class: Profile::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Utilities
Defined in:
lib/git/profile.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#fetch_user_data, #save, #user_exists?

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/git/profile.rb', line 55

def self.exit_on_failure?
  true
end

Instance Method Details

#addObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/git/profile.rb', line 60

def add
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  profile_directory = File.join(Dir.home, ".git-profile")
  username, email = fetch_user_data

  if File.exist?(profiles)
    save(username, email)
  else
    Dir.mkdir(profile_directory) unless Dir.exist?(profile_directory)
    create_file "#{File.join(Dir.home, ".git-profile/profiles.yml")}"
    username, email = fetch_user_data
    save(username, email)
  end
end

#delete(username) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/git/profile.rb', line 107

def delete(username)
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")

  if File.exist?(profiles) && !File.zero?(profiles)
    users = YAML.load(File.read(profiles))
    user = users[:users].filter { |user| user[:username] == username }

    unless user.empty?
      users[:users].delete(user.first)
      say("User #{user.first[:username]} <#{user.first[:email].to_s}> has been deleted!")
      if users[:users].empty?
        File.open(profiles, 'w') {|file| file.truncate(0) } 
      else
        File.open(profiles, "w") { |file| file.write(users.to_yaml) }
      end
    else
      say("No git profiles with username #{username} was found. You can see all available profiles with `$git-profile list` command .")
    end
  else
    say("No git profiles found. You can add a git profile with `$git-profile add` command .")
  end
end

#listObject



76
77
78
79
80
81
82
83
84
# File 'lib/git/profile.rb', line 76

def list
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  if File.exist?(profiles) && !File.zero?(profiles)
    users = YAML.load(File.read(profiles))
    users[:users].each_with_index { |user, index| say("#{index + 1}. #{user[:username]} <#{user[:email]}>") }
  else
    say("No git profiles found. You can add a git profile with `$git-profile add` command .")
  end
end

#resetObject



131
132
133
134
135
136
137
# File 'lib/git/profile.rb', line 131

def reset
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  config_path = File.join(Dir.home, ".gitconfig")
  File.open(profiles, 'w') {|file| file.truncate(0) } if File.exist?(profiles) && !File.zero?(profiles)
  File.open(config_path, 'w') {|file| file.truncate(0) } if File.exist?(config_path)
  say("reset successfull!")
end

#use(username) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git/profile.rb', line 87

def use(username)
  profiles = File.join(Dir.home, "/.git-profile/profiles.yml")
  config_path = File.join(Dir.home, ".gitconfig")
  if File.exist?(profiles) && !File.zero?(profiles)
    users = YAML.load(File.read(profiles))
    user = users[:users].filter { |user| user[:username] == username }        
    unless user.empty?
      File.open(config_path, 'w') {|file| file.truncate(0) } if File.exist?(config_path)
      run("git config --global user.name #{user.first[:username]}")
      run("git config --global user.email #{user.first[:email]}")
      say("git credentials has been updated.")
    else
      say("No git profiles with username #{username} was found. See all available profiles with `$git-profile list` command .")
    end
  else
    say("No git profiles with username #{username} was found. See all available profiles with `$git-profile list` command .")
  end
end

#whoamiObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/git/profile.rb', line 140

def whoami
  config_path = File.join(Dir.home, ".gitconfig")
  if File.exist?(config_path) && !File.zero?(config_path)
    print("username: ")
    username = run("git config user.name", config = {:verbose => false})
    print("email: ")
    email = run("git config user.email", config = {:verbose => false})
  else
    say("No git profiles found. You can add a git profile with `$git-profile use` command .")
  end
end