Michael Granger avatar Michael Granger committed df85fc9

* A bit more hacking on the OHM experiment.
* Treequel::Branch
- Made attribute values in #must_attributes_hash empty strings to distinguish them from
those created by #may_attributes_hash.
- Added #valid_attributes_hash as a convenience method for a merged `must_attributes_hash` and
`may_attributes_hash`.
* Made the collection class returned from Treequel::Directory#search settable via a
hash parameter, with fallback to the class of the base argument if it supports `new_from_entry`
or Treequel::Branch if not.
* Added RFC 4514 (Distinguished Names) productions to Treequel::Constants::Patterns
* Treequel shell:
- Changed the 'cd' command to 'cdn'
- Validate the RDN passed to 'cdn'
- Added a 'parent' command to allow changing up one RDN
- Added a 'log' command to allow setting of log level, default log level to 'WARN'

Comments (0)

Files changed (1)

 require 'abbrev'
 require 'treequel'
 require 'treequel/mixins'
+require 'treequel/constants'
 
 
 class Shell
-	include Treequel::Loggable
+	include Treequel::Loggable,
+	        Treequel::Constants::Patterns
+
 
 	### Create a new shell that will traverse the directory at the specified +uri+.
 	def initialize( uri )
-		Treequel.logger.level = Logger::DEBUG
+		Treequel.logger.level = Logger::WARN
 
 		@uri        = uri
 		@quit       = false
 			if input.nil?
 				@quit = true
 
+			elsif input == ''
+				self.log.debug "No command. Re-displaying the prompt."
+
 			# Parse everything else into command + everything else
 			else
 				command, *args = Shellwords.shellwords( input )
 	end
 
 
+	LOG_LEVELS = {
+		'debug' => Logger::DEBUG,
+		'info'  => Logger::INFO,
+		'warn'  => Logger::WARN,
+		'error' => Logger::ERROR,
+		'fatal' => Logger::FATAL,
+	}.freeze
+	LOG_LEVEL_NAMES = LOG_LEVELS.invert.freeze
+
+	### Set the logging level (if invoked with an argument) or display the current
+	### level (with no argument).
+	def log_command( *args )
+		newlevel = args.shift
+		if newlevel
+			if LOG_LEVELS.key?( newlevel )
+				Treequel.logger.level = LOG_LEVELS[ newlevel ]
+				$stderr.puts "Set log level to: %s" % [ newlevel ]
+			else
+				levelnames = LOG_LEVEL_NAMES.keys.sort.join(', ')
+				raise "Invalid log level %p: valid values are:\n   %s" % [ newlevel, levelnames ]
+			end
+		else
+			$stderr.puts "Log level is currently: %s" %
+				[ LOG_LEVEL_NAMES[Treequel.logger.level] ]
+		end
+	end
+
+
 	### Show the completions hash
 	def show_completions_command
 		$stderr.puts "Completions:",
 	end
 
 
-	### Change the current working DN.
-	def cd_command( *args )
-		rdn = args.shift
+	### Change the current working DN to +rdn+.
+	def cdn_command( rdn, *args )
+		raise "invalid RDN %p" % [ rdn ] unless RELATIVE_DISTINGUISHED_NAME.match( rdn )
+
 		pairs = rdn.split( /\s*,\s*/ )
 		pairs.each do |dnpair|
 			self.log.debug "  cd to %p" % [ dnpair ]
 	end
 
 
+	### Change the current working DN to the current entry's parent.
+	def parent_command( *args )
+		parent = @currbranch.parent or raise "%s is the root DN" % [ @currbranch.dn ]
+
+		self.log.debug "  changing to %s" % [ parent.dn ]
+		@currbranch = parent
+	end
+
+
+	### Edit the entry specified by +rdn+.
+	def edit_command( rdn, *args )
+		
+	end
+
+
 	### Handle a command from the user that doesn't exist.
 	def handle_missing_command( *args )
 		command = args.shift || '(testing?)'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.